aboutsummaryrefslogtreecommitdiff
path: root/cmd/fscrypt/fscrypt.go
blob: 65da7cba7f3de1d5153c8344961f7ef72aa18fb0 (plain)
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
/*
 * fscrypt.go - File which starts up and runs the application. Initializes
 * information about the application like the name, version, author, etc...
 *
 * Copyright 2017 Google Inc.
 * Author: Joe Richey (joerichey@google.com)
 *
 * 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.
 */

/*
fscrypt is a command line tool for managing linux filesystem encryption.
*/

package main

import (
	"fmt"

	"github.com/google/fscrypt/filesystem"
	"github.com/google/fscrypt/security"
	"github.com/pkg/errors"

	"github.com/google/fscrypt/actions"

	"github.com/google/fscrypt/cmd"
)

// Arguments used in fscrypt commands.
var (
	unusedMountpointArg = &cmd.Argument{
		Name:  "mountpoint",
		Usage: "path to a mountpoint on which to setup fscrypt",
	}
	usedMountpointArg = &cmd.Argument{
		Name:  "mountpoint",
		Usage: "path to a mountpoint being used with fscrypt",
	}
	directoryToEncryptArg = &cmd.Argument{
		Name:  "directory",
		Usage: "path to an empty directory to encrypt with fscrypt",
	}
	encryptedPathArg = &cmd.Argument{
		Name:  "path",
		Usage: "file or directory encrypted with fscrypt",
	}
)

func main() { fscryptCommand.Run() }

var fscryptCommand = cmd.Command{
	Title: "manage linux filesystem encryption",
	UsageLines: []string{
		fmt.Sprintf("<command> [arguments] [command options] [%s | %s]",
			cmd.VerboseFlag, cmd.QuietFlag),
		cmd.VersionUsage,
	},
	SubCommands: []*Command{
		setupCommand,
		encryptCommand,
		unlockCommand,
		purgeCommand,
		// statusCommand,
		// metadataCommand,
		cmd.VersionCommand,
	},
	Flags:   []cmd.Flag{cmd.VerboseFlag, cmd.QuietFlag, cmd.HelpFlag},
	ManPage: &cmd.ManPage{Name: "fscrypt", Section: 8},
}

// setup performs global or per-filesystem initialization of fscrypt data.
var setupCommand = &cmd.Command{
	Name:  "setup",
	Title: "setup a system/filesystem to use fscrypt",
	UsageLines: []string{
		fmt.Sprintf("[options]"),
		fmt.Sprintf("%s [%s]", unusedMountpointArg, cmd.ForceFlag),
	},
	Arguments:    []*cmd.Argument{unusedMountpointArg},
	InheritFlags: true,
	Flags:        []cmd.Flag{configFileFlag, targetFlag, legacyFlag, cmd.ForceFlag},
	ManPage:      &cmd.ManPage{Name: "fscrypt-setup", Section: 8},
	Action:       setupAction,
}

func setupAction(c *cmd.Context) error {
	switch len(c.Args) {
	case 0:
		// Case (1) - global setup
		return createGlobalConfig(configFileFlag.Value)
	case 1:
		// Case (2) - filesystem setup
		return setupFilesystem(c.Args[0])
	default:
		return cmd.CheckExpectedArgs(c, 1, true)
	}
}

// encrypt takes an empty directory, enables encryption, and unlocks it.
var encryptCommand = &cmd.Command{
	Name:         "encrypt",
	Title:        "start encrypting an empty directory",
	UsageLines:   nil, // TODO(joerichey)
	Arguments:    []*cmd.Argument{directoryToEncryptArg},
	InheritFlags: true,
	Flags: []cmd.Flag{sourceFlag, nameFlag, protectorFlag, policyFlag,
		keyFileFlag, userFlag, skipUnlockFlag},
	ManPage: &cmd.ManPage{Name: "fscrypt-encrypt", Section: 8},
	Action:  encryptAction,
}

func encryptAction(c *cmd.Context) error {
	if err := cmd.CheckExpectedArgs(c, 1, false); err != nil {
		return err
	}

	path := c.Args[0]
	if err := encryptPath(path); err != nil {
		return err
	}

	if !skipUnlockFlag.Value {
		fmt.Fprintf(cmd.Output, "%q is now encrypted, unlocked, and ready for use.\n", path)
		return nil
	}

	fmt.Fprintf(cmd.Output, "%q is now encrypted, but it is still locked.\n", path)
	fmt.Fprintf(cmd.Output, "It can be unlocked with: fscrypt unlock %q\n", path)
	return nil
}

// unlock takes an encrypted path and makes it available for reading/writing.
var unlockCommand = &cmd.Command{
	Name:         "unlock",
	Title:        "unlock an encrypted file or directory",
	UsageLines:   nil, // TODO(joerichey)
	Arguments:    []*cmd.Argument{encryptedPathArg},
	InheritFlags: true,
	Flags:        []cmd.Flag{protectorFlag, policyFlag, keyFileFlag, userFlag},
	ManPage:      &cmd.ManPage{Name: "fscrypt-unlock", Section: 8},
	Action:       unlockAction,
}

func unlockAction(c *cmd.Context) error {
	if err := cmd.CheckExpectedArgs(c, 1, false); err != nil {
		return err
	}

	path := c.Args[0]
	if err := unlockPath(path); err != nil {
		return err
	}

	fmt.Fprintf(cmd.Output, "%q is now unlocked and ready for use.\n", path)
	return nil
}

// purge removes all the policy keys from the keyring (my require unmount).
var purgeCommand = &cmd.Command{
	Name:  "purge",
	Title: "remove a directory's encryption keys",
	UsageLines: []string{fmt.Sprintf("%s, [%s=false] [%s] [%s]",
		usedMountpointArg, dropCachesFlag, userFlag, cmd.ForceFlag)},
	Arguments:    []*cmd.Argument{usedMountpointArg},
	InheritFlags: true,
	Flags:        []cmd.Flag{dropCachesFlag, userFlag, cmd.ForceFlag},
	ManPage:      &cmd.ManPage{Name: "fscrypt-purge", Section: 8},
	Action:       purgeAction,
}

func purgeAction(c *cmd.Context) error {
	if err := cmd.CheckExpectedArgs(c, 1, false); err != nil {
		return err
	}
	if dropCachesFlag.Value {
		if cmd.CheckIfRoot() != nil {
			return ErrDropCachesPerm
		}
	}

	targetUser, err := parseUserFlag(true)
	if err != nil {
		return err
	}
	ctx, err := actions.NewContextFromMountpoint(c.Args[0], target)
	if err != nil {
		return err
	}

	question := fmt.Sprintf("Purge all policy keys from %q", ctx.Mount.Path)
	if dropCachesFlag.Value {
		question += " and drop global inode cache"
	}
	warning := "Encrypted data on this filesystem will be inaccessible until unlocked again!!"
	if err = cmd.AskConfirmation(question+"?", warning, false); err != nil {
		return err
	}
	if err = actions.PurgeAllPolicies(ctx); err != nil {
		return err
	}
	fmt.Fprintf(cmd.Output, "Policies purged from filesystem %q.\n", ctx.Mount.Path)

	if !dropCachesFlag.Value {
		fmt.Fprintf(cmd.Output, "Filesystem %q should now be unmounted.\n", cmd.Mount.Path)
		return nil
	}
	if err = security.DropFilesystemCache(); err != nil {
		return err
	}
	fmt.Fprintln(cmd.Output, "Encrypted data removed from filesystem cache.")
	return nil
}

// status is a command that gets info about the system, a mountpoint, or a path.
var statusCommand = &cmd.Command{
	Name:       "status",
	Title:      "get the status of the system or a path",
	UsageLines: []string{"", usedMountpointArg.String(), encryptedPathArg.String()},
	Flags:      []cmd.Flag{cmd.VerboseFlag, cmd.HelpFlag},
	ManPage:    &cmd.ManPage{Name: "fscrypt-status", Section: 8},
	Action:     statusAction,
}

func statusAction(c *cmd.Context) error {
	switch len(c.Args) {
	case 0:
		// Case (1) - global status
		return writeGlobalStatus()
	case 1:
		path := c.Args[0]
		ctx, mntErr := actions.NewContextFromMountpoint(path, nil)

		switch errors.Cause(mntErr) {
		case nil:
			// Case (2) - mountpoint status
			return writeFilesystemStatus(ctx)
		case filesystem.ErrNotAMountpoint:
			// Case (3) - file or directory status
			return writePathStatus(path)
		default:
			return mntErr
		}
	default:
		return expectedArgsErr(c, 1, true)
	}
}