/
services.go
212 lines (188 loc) · 5.92 KB
/
services.go
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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2017 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package configcore
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/snapcore/snapd/boot"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/overlord/configstate/config"
"github.com/snapcore/snapd/systemd"
)
var services = []struct{ configName, systemdName string }{
{"ssh", "ssh.service"},
{"rsyslog", "rsyslog.service"},
{"console-conf", "console-conf@*"},
}
func init() {
for _, service := range services {
s := fmt.Sprintf("core.service.%s.disable", service.configName)
supportedConfigurations[s] = true
}
}
type sysdLogger struct{}
func (l *sysdLogger) Notify(status string) {
fmt.Fprintf(Stderr, "sysd: %s\n", status)
}
// switchDisableSSHService handles the special case of disabling/enabling ssh
// service on core devices.
func switchDisableSSHService(sysd systemd.Systemd, serviceName string, disabled bool, opts *fsOnlyContext) error {
rootDir := dirs.GlobalRootDir
if opts != nil {
rootDir = opts.RootDir
if err := os.MkdirAll(filepath.Join(rootDir, "/etc/ssh"), 0755); err != nil {
return err
}
}
sshCanary := filepath.Join(rootDir, "/etc/ssh/sshd_not_to_be_run")
if disabled {
if err := ioutil.WriteFile(sshCanary, []byte("SSH has been disabled by snapd system configuration\n"), 0644); err != nil {
return err
}
if opts == nil {
return sysd.Stop(serviceName, 5*time.Minute)
}
} else {
err := os.Remove(sshCanary)
if err != nil && !os.IsNotExist(err) {
return err
}
// Unmask both sshd.service and ssh.service and ignore the
// errors, if any. This undoes the damage done by earlier
// versions of snapd.
sysd.Unmask("sshd.service")
sysd.Unmask("ssh.service")
if opts == nil {
return sysd.Start(serviceName)
}
}
return nil
}
// switchDisableConsoleConfService handles the special case of
// disabling/enabling console-conf on core devices.
//
// Note that this option can only be changed via gadget defaults.
// It is not possible to tune this at runtime
func switchDisableConsoleConfService(sysd systemd.Systemd, serviceName string, disabled bool, opts *fsOnlyContext) error {
consoleConfDisabled := "/var/lib/console-conf/complete"
// at runtime we can not change this setting
if opts == nil {
// Special case: during install mode the
// gadget-defaults will also be set as part of the
// system install change. However during install mode
// console-conf has no "complete" file, it just never runs
// in install mode. So we need to detect this and do nothing
// or the install mode will fail.
// XXX: instead of this hack we should look at the config
// defaults and compare with the setting and exit if
// they are the same but that requires some more changes.
mode, _, _ := boot.ModeAndRecoverySystemFromKernelCommandLine()
if mode == boot.ModeInstall {
return nil
}
hasDisabledFile := osutil.FileExists(filepath.Join(dirs.GlobalRootDir, consoleConfDisabled))
if disabled != hasDisabledFile {
return fmt.Errorf("cannot toggle console-conf at runtime, but only initially via gadget defaults")
}
return nil
}
if !disabled {
return nil
}
// disable console-conf at the gadget-defaults time
consoleConfDisabled = filepath.Join(opts.RootDir, consoleConfDisabled)
if err := os.MkdirAll(filepath.Dir(consoleConfDisabled), 0755); err != nil {
return err
}
if err := ioutil.WriteFile(consoleConfDisabled, []byte("console-conf has been disabled by the snapd system configuration\n"), 0644); err != nil {
return err
}
return nil
}
// switchDisableTypicalService switches a service in/out of disabled state
// where "true" means disabled and "false" means enabled.
func switchDisableService(serviceName string, disabled bool, opts *fsOnlyContext) error {
var sysd systemd.Systemd
if opts != nil {
sysd = systemd.NewEmulationMode(opts.RootDir)
} else {
sysd = systemd.NewUnderRoot(dirs.GlobalRootDir, systemd.SystemMode, &sysdLogger{})
}
// some services are special
switch serviceName {
case "ssh.service":
return switchDisableSSHService(sysd, serviceName, disabled, opts)
case "console-conf@*":
return switchDisableConsoleConfService(sysd, serviceName, disabled, opts)
}
if disabled {
if opts == nil {
if err := sysd.Disable(serviceName); err != nil {
return err
}
}
if err := sysd.Mask(serviceName); err != nil {
return err
}
if opts == nil {
return sysd.Stop(serviceName, 5*time.Minute)
}
} else {
if err := sysd.Unmask(serviceName); err != nil {
return err
}
if opts == nil {
if err := sysd.Enable(serviceName); err != nil {
return err
}
}
if opts == nil {
return sysd.Start(serviceName)
}
}
return nil
}
// services that can be disabled
func handleServiceDisableConfiguration(tr config.ConfGetter, opts *fsOnlyContext) error {
for _, service := range services {
optionName := fmt.Sprintf("service.%s.disable", service.configName)
outputStr, err := coreCfg(tr, optionName)
if err != nil {
return err
}
if outputStr != "" {
var disabled bool
switch outputStr {
case "true":
disabled = true
case "false":
disabled = false
default:
return fmt.Errorf("option %q has invalid value %q", optionName, outputStr)
}
if err := switchDisableService(service.systemdName, disabled, opts); err != nil {
return err
}
}
}
return nil
}