portable.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // +build !noportable
  2. package cmd
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path"
  8. "path/filepath"
  9. "strings"
  10. "github.com/spf13/cobra"
  11. "github.com/drakkan/sftpgo/common"
  12. "github.com/drakkan/sftpgo/dataprovider"
  13. "github.com/drakkan/sftpgo/kms"
  14. "github.com/drakkan/sftpgo/service"
  15. "github.com/drakkan/sftpgo/sftpd"
  16. "github.com/drakkan/sftpgo/version"
  17. "github.com/drakkan/sftpgo/vfs"
  18. )
  19. var (
  20. directoryToServe string
  21. portableSFTPDPort int
  22. portableAdvertiseService bool
  23. portableAdvertiseCredentials bool
  24. portableUsername string
  25. portablePassword string
  26. portableLogFile string
  27. portableLogVerbose bool
  28. portablePublicKeys []string
  29. portablePermissions []string
  30. portableSSHCommands []string
  31. portableAllowedPatterns []string
  32. portableDeniedPatterns []string
  33. portableFsProvider int
  34. portableS3Bucket string
  35. portableS3Region string
  36. portableS3AccessKey string
  37. portableS3AccessSecret string
  38. portableS3Endpoint string
  39. portableS3StorageClass string
  40. portableS3KeyPrefix string
  41. portableS3ULPartSize int
  42. portableS3ULConcurrency int
  43. portableGCSBucket string
  44. portableGCSCredentialsFile string
  45. portableGCSAutoCredentials int
  46. portableGCSStorageClass string
  47. portableGCSKeyPrefix string
  48. portableFTPDPort int
  49. portableFTPSCert string
  50. portableFTPSKey string
  51. portableWebDAVPort int
  52. portableWebDAVCert string
  53. portableWebDAVKey string
  54. portableAzContainer string
  55. portableAzAccountName string
  56. portableAzAccountKey string
  57. portableAzEndpoint string
  58. portableAzAccessTier string
  59. portableAzSASURL string
  60. portableAzKeyPrefix string
  61. portableAzULPartSize int
  62. portableAzULConcurrency int
  63. portableAzUseEmulator bool
  64. portableCryptPassphrase string
  65. portableSFTPEndpoint string
  66. portableSFTPUsername string
  67. portableSFTPPassword string
  68. portableSFTPPrivateKeyPath string
  69. portableSFTPFingerprints []string
  70. portableSFTPPrefix string
  71. portableCmd = &cobra.Command{
  72. Use: "portable",
  73. Short: "Serve a single directory",
  74. Long: `To serve the current working directory with auto generated credentials simply
  75. use:
  76. $ sftpgo portable
  77. Please take a look at the usage below to customize the serving parameters`,
  78. Run: func(cmd *cobra.Command, args []string) {
  79. portableDir := directoryToServe
  80. fsProvider := dataprovider.FilesystemProvider(portableFsProvider)
  81. if !filepath.IsAbs(portableDir) {
  82. if fsProvider == dataprovider.LocalFilesystemProvider {
  83. portableDir, _ = filepath.Abs(portableDir)
  84. } else {
  85. portableDir = os.TempDir()
  86. }
  87. }
  88. permissions := make(map[string][]string)
  89. permissions["/"] = portablePermissions
  90. portableGCSCredentials := ""
  91. if fsProvider == dataprovider.GCSFilesystemProvider && portableGCSCredentialsFile != "" {
  92. contents, err := getFileContents(portableGCSCredentialsFile)
  93. if err != nil {
  94. fmt.Printf("Unable to get GCS credentials: %v\n", err)
  95. os.Exit(1)
  96. }
  97. portableGCSCredentials = contents
  98. portableGCSAutoCredentials = 0
  99. }
  100. portableSFTPPrivateKey := ""
  101. if fsProvider == dataprovider.SFTPFilesystemProvider && portableSFTPPrivateKeyPath != "" {
  102. contents, err := getFileContents(portableSFTPPrivateKeyPath)
  103. if err != nil {
  104. fmt.Printf("Unable to get SFTP private key: %v\n", err)
  105. os.Exit(1)
  106. }
  107. portableSFTPPrivateKey = contents
  108. }
  109. if portableFTPDPort >= 0 && len(portableFTPSCert) > 0 && len(portableFTPSKey) > 0 {
  110. _, err := common.NewCertManager(portableFTPSCert, portableFTPSKey, "FTP portable")
  111. if err != nil {
  112. fmt.Printf("Unable to load FTPS key pair, cert file %#v key file %#v error: %v\n",
  113. portableFTPSCert, portableFTPSKey, err)
  114. os.Exit(1)
  115. }
  116. }
  117. if portableWebDAVPort > 0 && len(portableWebDAVCert) > 0 && len(portableWebDAVKey) > 0 {
  118. _, err := common.NewCertManager(portableWebDAVCert, portableWebDAVKey, "WebDAV portable")
  119. if err != nil {
  120. fmt.Printf("Unable to load WebDAV key pair, cert file %#v key file %#v error: %v\n",
  121. portableWebDAVCert, portableWebDAVKey, err)
  122. os.Exit(1)
  123. }
  124. }
  125. service := service.Service{
  126. ConfigDir: filepath.Clean(defaultConfigDir),
  127. ConfigFile: defaultConfigFile,
  128. LogFilePath: portableLogFile,
  129. LogMaxSize: defaultLogMaxSize,
  130. LogMaxBackups: defaultLogMaxBackup,
  131. LogMaxAge: defaultLogMaxAge,
  132. LogCompress: defaultLogCompress,
  133. LogVerbose: portableLogVerbose,
  134. Shutdown: make(chan bool),
  135. PortableMode: 1,
  136. PortableUser: dataprovider.User{
  137. Username: portableUsername,
  138. Password: portablePassword,
  139. PublicKeys: portablePublicKeys,
  140. Permissions: permissions,
  141. HomeDir: portableDir,
  142. Status: 1,
  143. FsConfig: dataprovider.Filesystem{
  144. Provider: dataprovider.FilesystemProvider(portableFsProvider),
  145. S3Config: vfs.S3FsConfig{
  146. Bucket: portableS3Bucket,
  147. Region: portableS3Region,
  148. AccessKey: portableS3AccessKey,
  149. AccessSecret: kms.NewPlainSecret(portableS3AccessSecret),
  150. Endpoint: portableS3Endpoint,
  151. StorageClass: portableS3StorageClass,
  152. KeyPrefix: portableS3KeyPrefix,
  153. UploadPartSize: int64(portableS3ULPartSize),
  154. UploadConcurrency: portableS3ULConcurrency,
  155. },
  156. GCSConfig: vfs.GCSFsConfig{
  157. Bucket: portableGCSBucket,
  158. Credentials: kms.NewPlainSecret(portableGCSCredentials),
  159. AutomaticCredentials: portableGCSAutoCredentials,
  160. StorageClass: portableGCSStorageClass,
  161. KeyPrefix: portableGCSKeyPrefix,
  162. },
  163. AzBlobConfig: vfs.AzBlobFsConfig{
  164. Container: portableAzContainer,
  165. AccountName: portableAzAccountName,
  166. AccountKey: kms.NewPlainSecret(portableAzAccountKey),
  167. Endpoint: portableAzEndpoint,
  168. AccessTier: portableAzAccessTier,
  169. SASURL: portableAzSASURL,
  170. KeyPrefix: portableAzKeyPrefix,
  171. UseEmulator: portableAzUseEmulator,
  172. UploadPartSize: int64(portableAzULPartSize),
  173. UploadConcurrency: portableAzULConcurrency,
  174. },
  175. CryptConfig: vfs.CryptFsConfig{
  176. Passphrase: kms.NewPlainSecret(portableCryptPassphrase),
  177. },
  178. SFTPConfig: vfs.SFTPFsConfig{
  179. Endpoint: portableSFTPEndpoint,
  180. Username: portableSFTPUsername,
  181. Password: kms.NewPlainSecret(portableSFTPPassword),
  182. PrivateKey: kms.NewPlainSecret(portableSFTPPrivateKey),
  183. Fingerprints: portableSFTPFingerprints,
  184. Prefix: portableSFTPPrefix,
  185. },
  186. },
  187. Filters: dataprovider.UserFilters{
  188. FilePatterns: parsePatternsFilesFilters(),
  189. },
  190. },
  191. }
  192. if err := service.StartPortableMode(portableSFTPDPort, portableFTPDPort, portableWebDAVPort, portableSSHCommands, portableAdvertiseService,
  193. portableAdvertiseCredentials, portableFTPSCert, portableFTPSKey, portableWebDAVCert, portableWebDAVKey); err == nil {
  194. service.Wait()
  195. if service.Error == nil {
  196. os.Exit(0)
  197. }
  198. }
  199. os.Exit(1)
  200. },
  201. }
  202. )
  203. func init() {
  204. version.AddFeature("+portable")
  205. portableCmd.Flags().StringVarP(&directoryToServe, "directory", "d", ".", `Path to the directory to serve.
  206. This can be an absolute path or a path
  207. relative to the current directory
  208. `)
  209. portableCmd.Flags().IntVarP(&portableSFTPDPort, "sftpd-port", "s", 0, `0 means a random unprivileged port,
  210. < 0 disabled`)
  211. portableCmd.Flags().IntVar(&portableFTPDPort, "ftpd-port", -1, `0 means a random unprivileged port,
  212. < 0 disabled`)
  213. portableCmd.Flags().IntVar(&portableWebDAVPort, "webdav-port", -1, `0 means a random unprivileged port,
  214. < 0 disabled`)
  215. portableCmd.Flags().StringSliceVarP(&portableSSHCommands, "ssh-commands", "c", sftpd.GetDefaultSSHCommands(),
  216. `SSH commands to enable.
  217. "*" means any supported SSH command
  218. including scp
  219. `)
  220. portableCmd.Flags().StringVarP(&portableUsername, "username", "u", "", `Leave empty to use an auto generated
  221. value`)
  222. portableCmd.Flags().StringVarP(&portablePassword, "password", "p", "", `Leave empty to use an auto generated
  223. value`)
  224. portableCmd.Flags().StringVarP(&portableLogFile, logFilePathFlag, "l", "", "Leave empty to disable logging")
  225. portableCmd.Flags().BoolVarP(&portableLogVerbose, logVerboseFlag, "v", false, "Enable verbose logs")
  226. portableCmd.Flags().StringSliceVarP(&portablePublicKeys, "public-key", "k", []string{}, "")
  227. portableCmd.Flags().StringSliceVarP(&portablePermissions, "permissions", "g", []string{"list", "download"},
  228. `User's permissions. "*" means any
  229. permission`)
  230. portableCmd.Flags().StringArrayVar(&portableAllowedPatterns, "allowed-patterns", []string{},
  231. `Allowed file patterns case insensitive.
  232. The format is:
  233. /dir::pattern1,pattern2.
  234. For example: "/somedir::*.jpg,a*b?.png"`)
  235. portableCmd.Flags().StringArrayVar(&portableDeniedPatterns, "denied-patterns", []string{},
  236. `Denied file patterns case insensitive.
  237. The format is:
  238. /dir::pattern1,pattern2.
  239. For example: "/somedir::*.jpg,a*b?.png"`)
  240. portableCmd.Flags().BoolVarP(&portableAdvertiseService, "advertise-service", "S", false,
  241. `Advertise configured services using
  242. multicast DNS`)
  243. portableCmd.Flags().BoolVarP(&portableAdvertiseCredentials, "advertise-credentials", "C", false,
  244. `If the SFTP/FTP service is
  245. advertised via multicast DNS, this
  246. flag allows to put username/password
  247. inside the advertised TXT record`)
  248. portableCmd.Flags().IntVarP(&portableFsProvider, "fs-provider", "f", int(dataprovider.LocalFilesystemProvider), `0 => local filesystem
  249. 1 => AWS S3 compatible
  250. 2 => Google Cloud Storage
  251. 3 => Azure Blob Storage
  252. 4 => Encrypted local filesystem
  253. 5 => SFTP`)
  254. portableCmd.Flags().StringVar(&portableS3Bucket, "s3-bucket", "", "")
  255. portableCmd.Flags().StringVar(&portableS3Region, "s3-region", "", "")
  256. portableCmd.Flags().StringVar(&portableS3AccessKey, "s3-access-key", "", "")
  257. portableCmd.Flags().StringVar(&portableS3AccessSecret, "s3-access-secret", "", "")
  258. portableCmd.Flags().StringVar(&portableS3Endpoint, "s3-endpoint", "", "")
  259. portableCmd.Flags().StringVar(&portableS3StorageClass, "s3-storage-class", "", "")
  260. portableCmd.Flags().StringVar(&portableS3KeyPrefix, "s3-key-prefix", "", `Allows to restrict access to the
  261. virtual folder identified by this
  262. prefix and its contents`)
  263. portableCmd.Flags().IntVar(&portableS3ULPartSize, "s3-upload-part-size", 5, `The buffer size for multipart uploads
  264. (MB)`)
  265. portableCmd.Flags().IntVar(&portableS3ULConcurrency, "s3-upload-concurrency", 2, `How many parts are uploaded in
  266. parallel`)
  267. portableCmd.Flags().StringVar(&portableGCSBucket, "gcs-bucket", "", "")
  268. portableCmd.Flags().StringVar(&portableGCSStorageClass, "gcs-storage-class", "", "")
  269. portableCmd.Flags().StringVar(&portableGCSKeyPrefix, "gcs-key-prefix", "", `Allows to restrict access to the
  270. virtual folder identified by this
  271. prefix and its contents`)
  272. portableCmd.Flags().StringVar(&portableGCSCredentialsFile, "gcs-credentials-file", "", `Google Cloud Storage JSON credentials
  273. file`)
  274. portableCmd.Flags().IntVar(&portableGCSAutoCredentials, "gcs-automatic-credentials", 1, `0 means explicit credentials using
  275. a JSON credentials file, 1 automatic
  276. `)
  277. portableCmd.Flags().StringVar(&portableFTPSCert, "ftpd-cert", "", "Path to the certificate file for FTPS")
  278. portableCmd.Flags().StringVar(&portableFTPSKey, "ftpd-key", "", "Path to the key file for FTPS")
  279. portableCmd.Flags().StringVar(&portableWebDAVCert, "webdav-cert", "", `Path to the certificate file for WebDAV
  280. over HTTPS`)
  281. portableCmd.Flags().StringVar(&portableWebDAVKey, "webdav-key", "", `Path to the key file for WebDAV over
  282. HTTPS`)
  283. portableCmd.Flags().StringVar(&portableAzContainer, "az-container", "", "")
  284. portableCmd.Flags().StringVar(&portableAzAccountName, "az-account-name", "", "")
  285. portableCmd.Flags().StringVar(&portableAzAccountKey, "az-account-key", "", "")
  286. portableCmd.Flags().StringVar(&portableAzSASURL, "az-sas-url", "", `Shared access signature URL`)
  287. portableCmd.Flags().StringVar(&portableAzEndpoint, "az-endpoint", "", `Leave empty to use the default:
  288. "blob.core.windows.net"`)
  289. portableCmd.Flags().StringVar(&portableAzAccessTier, "az-access-tier", "", `Leave empty to use the default
  290. container setting`)
  291. portableCmd.Flags().StringVar(&portableAzKeyPrefix, "az-key-prefix", "", `Allows to restrict access to the
  292. virtual folder identified by this
  293. prefix and its contents`)
  294. portableCmd.Flags().IntVar(&portableAzULPartSize, "az-upload-part-size", 4, `The buffer size for multipart uploads
  295. (MB)`)
  296. portableCmd.Flags().IntVar(&portableAzULConcurrency, "az-upload-concurrency", 2, `How many parts are uploaded in
  297. parallel`)
  298. portableCmd.Flags().BoolVar(&portableAzUseEmulator, "az-use-emulator", false, "")
  299. portableCmd.Flags().StringVar(&portableCryptPassphrase, "crypto-passphrase", "", `Passphrase for encryption/decryption`)
  300. portableCmd.Flags().StringVar(&portableSFTPEndpoint, "sftp-endpoint", "", `SFTP endpoint as host:port for SFTP
  301. provider`)
  302. portableCmd.Flags().StringVar(&portableSFTPUsername, "sftp-username", "", `SFTP user for SFTP provider`)
  303. portableCmd.Flags().StringVar(&portableSFTPPassword, "sftp-password", "", `SFTP password for SFTP provider`)
  304. portableCmd.Flags().StringVar(&portableSFTPPrivateKeyPath, "sftp-key-path", "", `SFTP private key path for SFTP provider`)
  305. portableCmd.Flags().StringSliceVar(&portableSFTPFingerprints, "sftp-fingerprints", []string{}, `SFTP fingerprints to verify remote host
  306. key for SFTP provider`)
  307. portableCmd.Flags().StringVar(&portableSFTPPrefix, "sftp-prefix", "", `SFTP prefix allows restrict all
  308. operations to a given path within the
  309. remote SFTP server`)
  310. rootCmd.AddCommand(portableCmd)
  311. }
  312. func parsePatternsFilesFilters() []dataprovider.PatternsFilter {
  313. var patterns []dataprovider.PatternsFilter
  314. for _, val := range portableAllowedPatterns {
  315. p, exts := getPatternsFilterValues(strings.TrimSpace(val))
  316. if len(p) > 0 {
  317. patterns = append(patterns, dataprovider.PatternsFilter{
  318. Path: path.Clean(p),
  319. AllowedPatterns: exts,
  320. DeniedPatterns: []string{},
  321. })
  322. }
  323. }
  324. for _, val := range portableDeniedPatterns {
  325. p, exts := getPatternsFilterValues(strings.TrimSpace(val))
  326. if len(p) > 0 {
  327. found := false
  328. for index, e := range patterns {
  329. if path.Clean(e.Path) == path.Clean(p) {
  330. patterns[index].DeniedPatterns = append(patterns[index].DeniedPatterns, exts...)
  331. found = true
  332. break
  333. }
  334. }
  335. if !found {
  336. patterns = append(patterns, dataprovider.PatternsFilter{
  337. Path: path.Clean(p),
  338. AllowedPatterns: []string{},
  339. DeniedPatterns: exts,
  340. })
  341. }
  342. }
  343. }
  344. return patterns
  345. }
  346. func getPatternsFilterValues(value string) (string, []string) {
  347. if strings.Contains(value, "::") {
  348. dirExts := strings.Split(value, "::")
  349. if len(dirExts) > 1 {
  350. dir := strings.TrimSpace(dirExts[0])
  351. exts := []string{}
  352. for _, e := range strings.Split(dirExts[1], ",") {
  353. cleanedExt := strings.TrimSpace(e)
  354. if len(cleanedExt) > 0 {
  355. exts = append(exts, cleanedExt)
  356. }
  357. }
  358. if dir != "" && len(exts) > 0 {
  359. return dir, exts
  360. }
  361. }
  362. }
  363. return "", nil
  364. }
  365. func getFileContents(name string) (string, error) {
  366. fi, err := os.Stat(name)
  367. if err != nil {
  368. return "", err
  369. }
  370. if fi.Size() > 1048576 {
  371. return "", fmt.Errorf("%#v is too big %v/1048576 bytes", name, fi.Size())
  372. }
  373. contents, err := ioutil.ReadFile(name)
  374. if err != nil {
  375. return "", err
  376. }
  377. return string(contents), nil
  378. }