2015-06-25 21:57:10 -04:00
|
|
|
"""Contains UI methods for Apache operations."""
|
2015-06-26 17:47:03 -04:00
|
|
|
import logging
|
|
|
|
|
|
2015-06-25 20:02:09 -04:00
|
|
|
import zope.component
|
|
|
|
|
|
2016-04-13 19:30:57 -04:00
|
|
|
from certbot import errors
|
|
|
|
|
from certbot import interfaces
|
2019-04-12 16:32:52 -04:00
|
|
|
from certbot.compat import os
|
2019-12-09 15:50:20 -05:00
|
|
|
import certbot.display.util as display_util
|
2015-06-25 20:02:09 -04:00
|
|
|
|
2015-06-26 17:47:03 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
2018-02-28 14:31:47 -05:00
|
|
|
def select_vhost_multiple(vhosts):
|
|
|
|
|
"""Select multiple Vhosts to install the certificate for
|
|
|
|
|
|
|
|
|
|
:param vhosts: Available Apache VirtualHosts
|
|
|
|
|
:type vhosts: :class:`list` of type `~obj.Vhost`
|
|
|
|
|
|
|
|
|
|
:returns: List of VirtualHosts
|
|
|
|
|
:rtype: :class:`list`of type `~obj.Vhost`
|
|
|
|
|
"""
|
|
|
|
|
if not vhosts:
|
|
|
|
|
return list()
|
|
|
|
|
tags_list = [vhost.display_repr()+"\n" for vhost in vhosts]
|
|
|
|
|
# Remove the extra newline from the last entry
|
2018-05-14 12:33:30 -04:00
|
|
|
if tags_list:
|
2018-02-28 14:31:47 -05:00
|
|
|
tags_list[-1] = tags_list[-1][:-1]
|
|
|
|
|
code, names = zope.component.getUtility(interfaces.IDisplay).checklist(
|
|
|
|
|
"Which VirtualHosts would you like to install the wildcard certificate for?",
|
|
|
|
|
tags=tags_list, force_interactive=True)
|
|
|
|
|
if code == display_util.OK:
|
|
|
|
|
return_vhosts = _reversemap_vhosts(names, vhosts)
|
|
|
|
|
return return_vhosts
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
def _reversemap_vhosts(names, vhosts):
|
|
|
|
|
"""Helper function for select_vhost_multiple for mapping string
|
|
|
|
|
representations back to actual vhost objects"""
|
|
|
|
|
return_vhosts = list()
|
|
|
|
|
|
|
|
|
|
for selection in names:
|
|
|
|
|
for vhost in vhosts:
|
|
|
|
|
if vhost.display_repr().strip() == selection.strip():
|
|
|
|
|
return_vhosts.append(vhost)
|
|
|
|
|
return return_vhosts
|
|
|
|
|
|
2015-06-25 20:02:09 -04:00
|
|
|
def select_vhost(domain, vhosts):
|
|
|
|
|
"""Select an appropriate Apache Vhost.
|
|
|
|
|
|
2018-02-28 14:31:47 -05:00
|
|
|
:param vhosts: Available Apache VirtualHosts
|
2015-06-25 20:02:09 -04:00
|
|
|
:type vhosts: :class:`list` of type `~obj.Vhost`
|
|
|
|
|
|
2015-06-26 17:47:03 -04:00
|
|
|
:returns: VirtualHost or `None`
|
|
|
|
|
:rtype: `~obj.Vhost` or `None`
|
2015-06-25 20:02:09 -04:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
if not vhosts:
|
|
|
|
|
return None
|
2018-02-28 14:31:47 -05:00
|
|
|
code, tag = _vhost_menu(domain, vhosts)
|
|
|
|
|
if code == display_util.OK:
|
|
|
|
|
return vhosts[tag]
|
2018-05-14 12:33:30 -04:00
|
|
|
return None
|
2015-06-25 20:02:09 -04:00
|
|
|
|
|
|
|
|
def _vhost_menu(domain, vhosts):
|
|
|
|
|
"""Select an appropriate Apache Vhost.
|
|
|
|
|
|
|
|
|
|
:param vhosts: Available Apache Virtual Hosts
|
|
|
|
|
:type vhosts: :class:`list` of type `~obj.Vhost`
|
|
|
|
|
|
|
|
|
|
:returns: Display tuple - ('code', tag')
|
|
|
|
|
:rtype: `tuple`
|
|
|
|
|
|
|
|
|
|
"""
|
2015-06-26 17:47:03 -04:00
|
|
|
# Free characters in the line of display text (9 is for ' | ' formatting)
|
|
|
|
|
free_chars = display_util.WIDTH - len("HTTPS") - len("Enabled") - 9
|
|
|
|
|
|
|
|
|
|
if free_chars < 2:
|
|
|
|
|
logger.debug("Display size is too small for "
|
2019-11-25 12:44:40 -05:00
|
|
|
"certbot_apache._internal.display_ops._vhost_menu()")
|
2015-06-26 17:47:03 -04:00
|
|
|
# This runs the edge off the screen, but it doesn't cause an "error"
|
|
|
|
|
filename_size = 1
|
|
|
|
|
disp_name_size = 1
|
|
|
|
|
else:
|
|
|
|
|
# Filename is a bit more important and probably longer with 000-*
|
|
|
|
|
filename_size = int(free_chars * .6)
|
|
|
|
|
disp_name_size = free_chars - filename_size
|
|
|
|
|
|
2015-06-25 20:02:09 -04:00
|
|
|
choices = []
|
|
|
|
|
for vhost in vhosts:
|
2015-07-22 05:05:01 -04:00
|
|
|
if len(vhost.get_names()) == 1:
|
|
|
|
|
disp_name = next(iter(vhost.get_names()))
|
2018-05-14 12:33:30 -04:00
|
|
|
elif not vhost.get_names():
|
2015-06-25 20:02:09 -04:00
|
|
|
disp_name = ""
|
|
|
|
|
else:
|
|
|
|
|
disp_name = "Multiple Names"
|
|
|
|
|
|
|
|
|
|
choices.append(
|
2015-06-26 17:47:03 -04:00
|
|
|
"{fn:{fn_size}s} | {name:{name_size}s} | {https:5s} | "
|
|
|
|
|
"{active:7s}".format(
|
|
|
|
|
fn=os.path.basename(vhost.filep)[:filename_size],
|
|
|
|
|
name=disp_name[:disp_name_size],
|
|
|
|
|
https="HTTPS" if vhost.ssl else "",
|
|
|
|
|
active="Enabled" if vhost.enabled else "",
|
|
|
|
|
fn_size=filename_size,
|
|
|
|
|
name_size=disp_name_size)
|
2015-06-25 20:02:09 -04:00
|
|
|
)
|
|
|
|
|
|
2015-12-29 17:21:05 -05:00
|
|
|
try:
|
|
|
|
|
code, tag = zope.component.getUtility(interfaces.IDisplay).menu(
|
2016-01-15 19:04:38 -05:00
|
|
|
"We were unable to find a vhost with a ServerName "
|
|
|
|
|
"or Address of {0}.{1}Which virtual host would you "
|
2018-07-31 12:56:03 -04:00
|
|
|
"like to choose?".format(domain, os.linesep),
|
2017-06-15 20:14:38 -04:00
|
|
|
choices, force_interactive=True)
|
2016-06-25 13:51:14 -04:00
|
|
|
except errors.MissingCommandlineFlag:
|
2017-06-09 15:48:59 -04:00
|
|
|
msg = (
|
2017-09-15 19:51:06 -04:00
|
|
|
"Encountered vhost ambiguity when trying to find a vhost for "
|
|
|
|
|
"{0} but was unable to ask for user "
|
2017-06-09 15:48:59 -04:00
|
|
|
"guidance in non-interactive mode. Certbot may need "
|
|
|
|
|
"vhosts to be explicitly labelled with ServerName or "
|
2017-09-15 19:51:06 -04:00
|
|
|
"ServerAlias directives.".format(domain))
|
2016-07-19 13:13:50 -04:00
|
|
|
logger.warning(msg)
|
2016-01-30 22:53:50 -05:00
|
|
|
raise errors.MissingCommandlineFlag(msg)
|
2015-06-25 20:02:09 -04:00
|
|
|
|
|
|
|
|
return code, tag
|