Skip to content

Commit 5011002

Browse files
committed
allow to set umask on *NIX platforms
Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
1 parent f5f5612 commit 5011002

File tree

5 files changed

+75
-1
lines changed

5 files changed

+75
-1
lines changed

docs/full-configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ The configuration file contains the following sections:
8686
- `max_per_host_connections`, integer. Maximum number of concurrent client connections from the same host (IP). If the defender is enabled, exceeding this limit will generate `score_limit_exceeded` events and thus hosts that repeatedly exceed the max allowed connections can be automatically blocked. 0 means unlimited. Default: `20`.
8787
- `allowlist_status`, integer. Set to `1` to enable the allow list. The allow list can be populated using the WebAdmin or the REST API. If enabled, only the listed IPs/networks can access the configured services, all other client connections will be dropped before they even try to authenticate. Ensure to populate your allow list before enabling this setting. In multi-nodes setups, the list entries propagation between nodes may take some minutes. Default: `0`.
8888
- `allow_self_connections`, integer. Allow users on this instance to use other users/virtual folders on this instance as storage backend. Enable this setting if you know what you are doing. Set to `1` to enable. Default: `0`.
89+
- `umask`, string. Set the file mode creation mask, for example `002`. Leave blank to use the system umask. Supported on *NIX platforms. Default: blank.
8990
- `defender`, struct containing the defender configuration. See [Defender](./defender.md) for more details.
9091
- `enabled`, boolean. Default `false`.
9192
- `driver`, string. Supported drivers are `memory` and `provider`. The `provider` driver will use the configured data provider to store defender events and it is supported for `MySQL`, `PostgreSQL` and `CockroachDB` data providers. Using the `provider` driver you can share the defender events among multiple SFTPGO instances. For a single instance the `memory` driver will be much faster. Default: `memory`.

internal/common/common.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ var (
166166
// Initialize sets the common configuration
167167
func Initialize(c Configuration, isShared int) error {
168168
isShuttingDown.Store(false)
169+
util.SetUmask(c.Umask)
169170
Config = c
170171
Config.Actions.ExecuteOn = util.RemoveDuplicates(Config.Actions.ExecuteOn, true)
171172
Config.Actions.ExecuteSync = util.RemoveDuplicates(Config.Actions.ExecuteSync, true)
@@ -569,7 +570,9 @@ type Configuration struct {
569570
// Defender configuration
570571
DefenderConfig DefenderConfig `json:"defender" mapstructure:"defender"`
571572
// Rate limiter configurations
572-
RateLimitersConfig []RateLimiterConfig `json:"rate_limiters" mapstructure:"rate_limiters"`
573+
RateLimitersConfig []RateLimiterConfig `json:"rate_limiters" mapstructure:"rate_limiters"`
574+
// Umask for new uploads. Leave blank to use the system default.
575+
Umask string `json:"umask" mapstructure:"umask"`
573576
idleTimeoutAsDuration time.Duration
574577
idleLoginTimeout time.Duration
575578
defender Defender

internal/util/util_fallback.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (C) 2019-2023 Nicola Murino
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU Affero General Public License as published
5+
// by the Free Software Foundation, version 3.
6+
//
7+
// This program is distributed in the hope that it will be useful,
8+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
// GNU Affero General Public License for more details.
11+
//
12+
// You should have received a copy of the GNU Affero General Public License
13+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
14+
15+
//go:build !unix
16+
17+
package util
18+
19+
import (
20+
"runtime"
21+
22+
"github.com/drakkan/sftpgo/v2/internal/logger"
23+
)
24+
25+
// SetUmask sets the specified umask
26+
func SetUmask(val string) {
27+
if val == "" {
28+
return
29+
}
30+
logger.Debug(logSender, "", "umask not supported on OS %q", runtime.GOOS)
31+
}

internal/util/util_unix.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (C) 2019-2023 Nicola Murino
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU Affero General Public License as published
5+
// by the Free Software Foundation, version 3.
6+
//
7+
// This program is distributed in the hope that it will be useful,
8+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
// GNU Affero General Public License for more details.
11+
//
12+
// You should have received a copy of the GNU Affero General Public License
13+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
14+
15+
//go:build unix
16+
17+
package util
18+
19+
import (
20+
"strconv"
21+
"syscall"
22+
23+
"github.com/drakkan/sftpgo/v2/internal/logger"
24+
)
25+
26+
// SetUmask sets the specified umask
27+
func SetUmask(val string) {
28+
if val == "" {
29+
return
30+
}
31+
umask, err := strconv.ParseUint(val, 8, 31)
32+
if err != nil {
33+
logger.Error(logSender, "", "invalid umask %q: %v", val, err)
34+
return
35+
}
36+
logger.Debug(logSender, "", "set umask to: %d, configured value: %q", umask, val)
37+
syscall.Umask(int(umask))
38+
}

sftpgo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"max_per_host_connections": 20,
2222
"allowlist_status": 0,
2323
"allow_self_connections": 0,
24+
"umask": "",
2425
"defender": {
2526
"enabled": false,
2627
"driver": "memory",

0 commit comments

Comments
 (0)