Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

"""Webroot plugin.""" 

import argparse 

import collections 

import errno 

import json 

import logging 

import os 

 

import six 

import zope.component 

import zope.interface 

 

from acme import challenges # pylint: disable=unused-import 

# pylint: disable=unused-import, no-name-in-module 

from acme.magic_typing import Dict, Set, DefaultDict, List 

# pylint: enable=unused-import, no-name-in-module 

 

from certbot import achallenges # pylint: disable=unused-import 

from certbot import cli 

from certbot import errors 

from certbot import interfaces 

from certbot.display import util as display_util 

from certbot.display import ops 

from certbot.plugins import common 

from certbot.plugins import util 

 

 

logger = logging.getLogger(__name__) 

 

 

@zope.interface.implementer(interfaces.IAuthenticator) 

@zope.interface.provider(interfaces.IPluginFactory) 

class Authenticator(common.Plugin): 

"""Webroot Authenticator.""" 

 

description = "Place files in webroot directory" 

 

MORE_INFO = """\ 

Authenticator plugin that performs http-01 challenge by saving 

necessary validation resources to appropriate paths on the file 

system. It expects that there is some other HTTP server configured 

to serve all files under specified web root ({0}).""" 

 

def more_info(self): # pylint: disable=missing-docstring,no-self-use 

return self.MORE_INFO.format(self.conf("path")) 

 

@classmethod 

def add_parser_arguments(cls, add): 

add("path", "-w", default=[], action=_WebrootPathAction, 

help="public_html / webroot path. This can be specified multiple " 

"times to handle different domains; each domain will have " 

"the webroot path that preceded it. For instance: `-w " 

"/var/www/example -d example.com -d www.example.com -w " 

"/var/www/thing -d thing.net -d m.thing.net` (default: Ask)") 

add("map", default={}, action=_WebrootMapAction, 

help="JSON dictionary mapping domains to webroot paths; this " 

"implies -d for each entry. You may need to escape this from " 

"your shell. E.g.: --webroot-map " 

'\'{"eg1.is,m.eg1.is":"/www/eg1/", "eg2.is":"/www/eg2"}\' ' 

"This option is merged with, but takes precedence over, -w / " 

"-d entries. At present, if you put webroot-map in a config " 

"file, it needs to be on a single line, like: webroot-map = " 

'{"example.com":"/var/www"}.') 

 

def get_chall_pref(self, domain): # pragma: no cover 

# pylint: disable=missing-docstring,no-self-use,unused-argument 

return [challenges.HTTP01] 

 

def __init__(self, *args, **kwargs): 

super(Authenticator, self).__init__(*args, **kwargs) 

self.full_roots = {} # type: Dict[str, str] 

self.performed = collections.defaultdict(set) \ 

# type: DefaultDict[str, Set[achallenges.KeyAuthorizationAnnotatedChallenge]] 

# stack of dirs successfully created by this authenticator 

self._created_dirs = [] # type: List[str] 

 

def prepare(self): # pylint: disable=missing-docstring 

pass 

 

def perform(self, achalls): # pylint: disable=missing-docstring 

self._set_webroots(achalls) 

 

self._create_challenge_dirs() 

 

return [self._perform_single(achall) for achall in achalls] 

 

def _set_webroots(self, achalls): 

if self.conf("path"): 

webroot_path = self.conf("path")[-1] 

logger.info("Using the webroot path %s for all unmatched domains.", 

webroot_path) 

for achall in achalls: 

self.conf("map").setdefault(achall.domain, webroot_path) 

else: 

known_webroots = list(set(six.itervalues(self.conf("map")))) 

for achall in achalls: 

if achall.domain not in self.conf("map"): 

new_webroot = self._prompt_for_webroot(achall.domain, 

known_webroots) 

# Put the most recently input 

# webroot first for easy selection 

try: 

known_webroots.remove(new_webroot) 

except ValueError: 

pass 

known_webroots.insert(0, new_webroot) 

self.conf("map")[achall.domain] = new_webroot 

 

def _prompt_for_webroot(self, domain, known_webroots): 

webroot = None 

 

while webroot is None: 

if known_webroots: 

# Only show the menu if we have options for it 

webroot = self._prompt_with_webroot_list(domain, known_webroots) 

if webroot is None: 

webroot = self._prompt_for_new_webroot(domain) 

else: 

# Allow prompt to raise PluginError instead of looping forever 

webroot = self._prompt_for_new_webroot(domain, True) 

 

return webroot 

 

def _prompt_with_webroot_list(self, domain, known_webroots): 

display = zope.component.getUtility(interfaces.IDisplay) 

path_flag = "--" + self.option_name("path") 

 

while True: 

code, index = display.menu( 

"Select the webroot for {0}:".format(domain), 

["Enter a new webroot"] + known_webroots, 

cli_flag=path_flag, force_interactive=True) 

if code == display_util.CANCEL: 

raise errors.PluginError( 

"Every requested domain must have a " 

"webroot when using the webroot plugin.") 

else: # code == display_util.OK 

return None if index == 0 else known_webroots[index - 1] 

 

def _prompt_for_new_webroot(self, domain, allowraise=False): 

code, webroot = ops.validated_directory( 

_validate_webroot, 

"Input the webroot for {0}:".format(domain), 

force_interactive=True) 

if code == display_util.CANCEL: 

if not allowraise: 

return None 

else: 

raise errors.PluginError( 

"Every requested domain must have a " 

"webroot when using the webroot plugin.") 

else: # code == display_util.OK 

return _validate_webroot(webroot) 

 

def _create_challenge_dirs(self): 

path_map = self.conf("map") 

if not path_map: 

raise errors.PluginError( 

"Missing parts of webroot configuration; please set either " 

"--webroot-path and --domains, or --webroot-map. Run with " 

" --help webroot for examples.") 

for name, path in path_map.items(): 

self.full_roots[name] = os.path.join(path, challenges.HTTP01.URI_ROOT_PATH) 

logger.debug("Creating root challenges validation dir at %s", 

self.full_roots[name]) 

 

# Change the permissions to be writable (GH #1389) 

# Umask is used instead of chmod to ensure the client can also 

# run as non-root (GH #1795) 

old_umask = os.umask(0o022) 

try: 

stat_path = os.stat(path) 

# We ignore the last prefix in the next iteration, 

# as it does not correspond to a folder path ('/' or 'C:') 

for prefix in sorted(util.get_prefixes(self.full_roots[name])[:-1], key=len): 

try: 

# This is coupled with the "umask" call above because 

# os.mkdir's "mode" parameter may not always work: 

# https://docs.python.org/3/library/os.html#os.mkdir 

os.mkdir(prefix, 0o0755) 

self._created_dirs.append(prefix) 

# Set owner as parent directory if possible 

try: 

os.chown(prefix, stat_path.st_uid, stat_path.st_gid) 

except (OSError, AttributeError) as exception: 

logger.info("Unable to change owner and uid of webroot directory") 

logger.debug("Error was: %s", exception) 

except OSError as exception: 

if exception.errno not in (errno.EEXIST, errno.EISDIR): 

raise errors.PluginError( 

"Couldn't create root for {0} http-01 " 

"challenge responses: {1}".format(name, exception)) 

finally: 

os.umask(old_umask) 

 

def _get_validation_path(self, root_path, achall): 

return os.path.join(root_path, achall.chall.encode("token")) 

 

def _perform_single(self, achall): 

response, validation = achall.response_and_validation() 

 

root_path = self.full_roots[achall.domain] 

validation_path = self._get_validation_path(root_path, achall) 

logger.debug("Attempting to save validation to %s", validation_path) 

 

# Change permissions to be world-readable, owner-writable (GH #1795) 

old_umask = os.umask(0o022) 

 

try: 

with open(validation_path, "wb") as validation_file: 

validation_file.write(validation.encode()) 

finally: 

os.umask(old_umask) 

 

self.performed[root_path].add(achall) 

return response 

 

def cleanup(self, achalls): # pylint: disable=missing-docstring 

for achall in achalls: 

root_path = self.full_roots.get(achall.domain, None) 

if root_path is not None: 

validation_path = self._get_validation_path(root_path, achall) 

logger.debug("Removing %s", validation_path) 

os.remove(validation_path) 

self.performed[root_path].remove(achall) 

 

not_removed = [] # type: List[str] 

while len(self._created_dirs) > 0: 

path = self._created_dirs.pop() 

try: 

os.rmdir(path) 

except OSError as exc: 

not_removed.insert(0, path) 

logger.info("Challenge directory %s was not empty, didn't remove", path) 

logger.debug("Error was: %s", exc) 

self._created_dirs = not_removed 

logger.debug("All challenges cleaned up") 

 

 

class _WebrootMapAction(argparse.Action): 

"""Action class for parsing webroot_map.""" 

 

def __call__(self, parser, namespace, webroot_map, option_string=None): 

for domains, webroot_path in six.iteritems(json.loads(webroot_map)): 

webroot_path = _validate_webroot(webroot_path) 

namespace.webroot_map.update( 

(d, webroot_path) for d in cli.add_domains(namespace, domains)) 

 

 

class _WebrootPathAction(argparse.Action): 

"""Action class for parsing webroot_path.""" 

 

def __init__(self, *args, **kwargs): 

super(_WebrootPathAction, self).__init__(*args, **kwargs) 

self._domain_before_webroot = False 

 

def __call__(self, parser, namespace, webroot_path, option_string=None): 

if self._domain_before_webroot: 

raise errors.PluginError( 

"If you specify multiple webroot paths, " 

"one of them must precede all domain flags") 

 

if namespace.webroot_path: 

# Apply previous webroot to all matched 

# domains before setting the new webroot path 

prev_webroot = namespace.webroot_path[-1] 

for domain in namespace.domains: 

namespace.webroot_map.setdefault(domain, prev_webroot) 

elif namespace.domains: 

self._domain_before_webroot = True 

 

namespace.webroot_path.append(_validate_webroot(webroot_path)) 

 

 

def _validate_webroot(webroot_path): 

"""Validates and returns the absolute path of webroot_path. 

 

:param str webroot_path: path to the webroot directory 

 

:returns: absolute path of webroot_path 

:rtype: str 

 

""" 

if not os.path.isdir(webroot_path): 

raise errors.PluginError(webroot_path + " does not exist or is not a directory") 

 

return os.path.abspath(webroot_path)