utils.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // Package utils provides some common utility methods
  2. package utils
  3. import (
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/ecdsa"
  7. "crypto/elliptic"
  8. "crypto/rand"
  9. "crypto/rsa"
  10. "crypto/x509"
  11. "encoding/hex"
  12. "encoding/pem"
  13. "errors"
  14. "fmt"
  15. "html/template"
  16. "io"
  17. "io/ioutil"
  18. "net"
  19. "os"
  20. "path"
  21. "path/filepath"
  22. "runtime"
  23. "strings"
  24. "time"
  25. "golang.org/x/crypto/ssh"
  26. "github.com/drakkan/sftpgo/logger"
  27. )
  28. const logSender = "utils"
  29. // IsStringInSlice searches a string in a slice and returns true if the string is found
  30. func IsStringInSlice(obj string, list []string) bool {
  31. for _, v := range list {
  32. if v == obj {
  33. return true
  34. }
  35. }
  36. return false
  37. }
  38. // IsStringPrefixInSlice searches a string prefix in a slice and returns true
  39. // if a matching prefix is found
  40. func IsStringPrefixInSlice(obj string, list []string) bool {
  41. for _, v := range list {
  42. if strings.HasPrefix(obj, v) {
  43. return true
  44. }
  45. }
  46. return false
  47. }
  48. // GetTimeAsMsSinceEpoch returns unix timestamp as milliseconds from a time struct
  49. func GetTimeAsMsSinceEpoch(t time.Time) int64 {
  50. return t.UnixNano() / 1000000
  51. }
  52. // GetTimeFromMsecSinceEpoch return a time struct from a unix timestamp with millisecond precision
  53. func GetTimeFromMsecSinceEpoch(msec int64) time.Time {
  54. return time.Unix(0, msec*1000000)
  55. }
  56. // GetDurationAsString returns a string representation for a time.Duration
  57. func GetDurationAsString(d time.Duration) string {
  58. d = d.Round(time.Second)
  59. h := d / time.Hour
  60. d -= h * time.Hour
  61. m := d / time.Minute
  62. d -= m * time.Minute
  63. s := d / time.Second
  64. if h > 0 {
  65. return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
  66. }
  67. return fmt.Sprintf("%02d:%02d", m, s)
  68. }
  69. // ByteCountSI returns humanized size in SI (decimal) format
  70. func ByteCountSI(b int64) string {
  71. return byteCount(b, 1000)
  72. }
  73. // ByteCountIEC returns humanized size in IEC (binary) format
  74. func ByteCountIEC(b int64) string {
  75. return byteCount(b, 1024)
  76. }
  77. func byteCount(b int64, unit int64) string {
  78. if b < unit {
  79. return fmt.Sprintf("%d B", b)
  80. }
  81. div, exp := unit, 0
  82. for n := b / unit; n >= unit; n /= unit {
  83. div *= unit
  84. exp++
  85. }
  86. if unit == 1000 {
  87. return fmt.Sprintf("%.1f %cB",
  88. float64(b)/float64(div), "KMGTPE"[exp])
  89. }
  90. return fmt.Sprintf("%.1f %ciB",
  91. float64(b)/float64(div), "KMGTPE"[exp])
  92. }
  93. // GetIPFromRemoteAddress returns the IP from the remote address.
  94. // If the given remote address cannot be parsed it will be returned unchanged
  95. func GetIPFromRemoteAddress(remoteAddress string) string {
  96. ip, _, err := net.SplitHostPort(remoteAddress)
  97. if err == nil {
  98. return ip
  99. }
  100. return remoteAddress
  101. }
  102. // NilIfEmpty returns nil if the input string is empty
  103. func NilIfEmpty(s string) *string {
  104. if len(s) == 0 {
  105. return nil
  106. }
  107. return &s
  108. }
  109. // EncryptData encrypts data using the given key
  110. func EncryptData(data string) (string, error) {
  111. var result string
  112. key := make([]byte, 16)
  113. if _, err := io.ReadFull(rand.Reader, key); err != nil {
  114. return result, err
  115. }
  116. keyHex := hex.EncodeToString(key)
  117. block, err := aes.NewCipher([]byte(keyHex))
  118. if err != nil {
  119. return result, err
  120. }
  121. gcm, err := cipher.NewGCM(block)
  122. if err != nil {
  123. return result, err
  124. }
  125. nonce := make([]byte, gcm.NonceSize())
  126. if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
  127. return result, err
  128. }
  129. ciphertext := gcm.Seal(nonce, nonce, []byte(data), nil)
  130. result = fmt.Sprintf("$aes$%s$%x", keyHex, ciphertext)
  131. return result, err
  132. }
  133. // RemoveDecryptionKey returns encrypted data without the decryption key
  134. func RemoveDecryptionKey(encryptData string) string {
  135. vals := strings.Split(encryptData, "$")
  136. if len(vals) == 4 {
  137. return fmt.Sprintf("$%v$%v", vals[1], vals[3])
  138. }
  139. return encryptData
  140. }
  141. // DecryptData decrypts data encrypted using EncryptData
  142. func DecryptData(data string) (string, error) {
  143. var result string
  144. vals := strings.Split(data, "$")
  145. if len(vals) != 4 {
  146. return "", errors.New("data to decrypt is not in the correct format")
  147. }
  148. key := vals[2]
  149. encrypted, err := hex.DecodeString(vals[3])
  150. if err != nil {
  151. return result, err
  152. }
  153. block, err := aes.NewCipher([]byte(key))
  154. if err != nil {
  155. return result, err
  156. }
  157. gcm, err := cipher.NewGCM(block)
  158. if err != nil {
  159. return result, err
  160. }
  161. nonceSize := gcm.NonceSize()
  162. nonce, ciphertext := encrypted[:nonceSize], encrypted[nonceSize:]
  163. plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
  164. if err != nil {
  165. return result, err
  166. }
  167. return string(plaintext), nil
  168. }
  169. // GenerateRSAKeys generate rsa private and public keys and write the
  170. // private key to specified file and the public key to the specified
  171. // file adding the .pub suffix
  172. func GenerateRSAKeys(file string) error {
  173. key, err := rsa.GenerateKey(rand.Reader, 4096)
  174. if err != nil {
  175. return err
  176. }
  177. o, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  178. if err != nil {
  179. return err
  180. }
  181. defer o.Close()
  182. priv := &pem.Block{
  183. Type: "RSA PRIVATE KEY",
  184. Bytes: x509.MarshalPKCS1PrivateKey(key),
  185. }
  186. if err := pem.Encode(o, priv); err != nil {
  187. return err
  188. }
  189. pub, err := ssh.NewPublicKey(&key.PublicKey)
  190. if err != nil {
  191. return err
  192. }
  193. return ioutil.WriteFile(file+".pub", ssh.MarshalAuthorizedKey(pub), 0600)
  194. }
  195. // GenerateECDSAKeys generate ecdsa private and public keys and write the
  196. // private key to specified file and the public key to the specified
  197. // file adding the .pub suffix
  198. func GenerateECDSAKeys(file string) error {
  199. key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  200. if err != nil {
  201. return err
  202. }
  203. o, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  204. if err != nil {
  205. return err
  206. }
  207. defer o.Close()
  208. keyBytes, err := x509.MarshalECPrivateKey(key)
  209. if err != nil {
  210. return err
  211. }
  212. priv := &pem.Block{
  213. Type: "EC PRIVATE KEY",
  214. Bytes: keyBytes,
  215. }
  216. if err := pem.Encode(o, priv); err != nil {
  217. return err
  218. }
  219. pub, err := ssh.NewPublicKey(&key.PublicKey)
  220. if err != nil {
  221. return err
  222. }
  223. return ioutil.WriteFile(file+".pub", ssh.MarshalAuthorizedKey(pub), 0600)
  224. }
  225. // GetDirsForSFTPPath returns all the directory for the given path in reverse order
  226. // for example if the path is: /1/2/3/4 it returns:
  227. // [ "/1/2/3/4", "/1/2/3", "/1/2", "/1", "/" ]
  228. func GetDirsForSFTPPath(p string) []string {
  229. sftpPath := CleanSFTPPath(p)
  230. dirsForPath := []string{sftpPath}
  231. for {
  232. if sftpPath == "/" {
  233. break
  234. }
  235. sftpPath = path.Dir(sftpPath)
  236. dirsForPath = append(dirsForPath, sftpPath)
  237. }
  238. return dirsForPath
  239. }
  240. // CleanSFTPPath returns a clean sftp path
  241. func CleanSFTPPath(p string) string {
  242. sftpPath := filepath.ToSlash(p)
  243. if !path.IsAbs(p) {
  244. sftpPath = "/" + sftpPath
  245. }
  246. return path.Clean(sftpPath)
  247. }
  248. // LoadTemplate wraps a call to a function returning (*Template, error)
  249. // it is just like template.Must but it writes a log before exiting
  250. func LoadTemplate(t *template.Template, err error) *template.Template {
  251. if err != nil {
  252. logger.ErrorToConsole("error loading required template: %v", err)
  253. logger.Error(logSender, "", "error loading required template: %v", err)
  254. panic(err)
  255. }
  256. return t
  257. }
  258. // IsFileInputValid returns true this is a valid file name.
  259. // This method must be used before joining a file name, generally provided as
  260. // user input, with a directory
  261. func IsFileInputValid(fileInput string) bool {
  262. cleanInput := filepath.Clean(fileInput)
  263. if cleanInput == "." || cleanInput == ".." {
  264. return false
  265. }
  266. return true
  267. }
  268. // CleanDirInput sanitizes user input for directories.
  269. // On Windows it removes any trailing `"`.
  270. // We try to help windows users that set an invalid path such as "C:\ProgramData\SFTPGO\".
  271. // This will only help if the invalid path is the last argument, for example in this command:
  272. // sftpgo.exe serve -c "C:\ProgramData\SFTPGO\" -l "sftpgo.log"
  273. // the -l flag will be ignored and the -c flag will get the value `C:\ProgramData\SFTPGO" -l sftpgo.log`
  274. // since the backslash after SFTPGO escape the double quote. This is definitely a bad user input
  275. func CleanDirInput(dirInput string) string {
  276. if runtime.GOOS == "windows" {
  277. for strings.HasSuffix(dirInput, "\"") {
  278. dirInput = strings.TrimSuffix(dirInput, "\"")
  279. }
  280. }
  281. return filepath.Clean(dirInput)
  282. }