/usr/lib/python2.7/dist-packages/nss_cache/config.py is in nsscache 0.32-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | # Copyright 2007 Google Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# 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, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""Configuration classes for nss_cache module.
These classes perform command line and file-based configuration
loading and parsing for the nss_cache module.
"""
__author__ = 'vasilios@google.com (Vasilios Hoffman)'
import ConfigParser
import logging
import re
# known nss map types.
MAP_PASSWORD = 'passwd'
MAP_GROUP = 'group'
MAP_SHADOW = 'shadow'
MAP_NETGROUP = 'netgroup'
MAP_AUTOMOUNT = 'automount'
MAP_SSHKEY = 'sshkey'
# accepted commands.
CMD_HELP = 'help'
CMD_REPAIR = 'repair'
CMD_STATUS = 'status'
CMD_UPDATE = 'update'
CMD_VERIFY = 'verify'
# default file locations
FILE_NSSWITCH = '/etc/nsswitch.conf'
# update method types
UPDATER_FILE = 'file'
UPDATER_MAP = 'map'
class Config(object):
"""Data container for runtime configuration information.
Global information such as the command, configured maps, etc, are
loaded into this object. Source and cache configuration
information is also stored here.
However since each map can be configured against a different
source and cache implementation we have to store per-map
configuration information. This is done via a Config().options
dictionary with the map name as the key and a MapOptions object as
the value.
"""
# default config file.
NSSCACHE_CONFIG = '/etc/nsscache.conf'
# known config file option names
OPT_SOURCE = 'source'
OPT_CACHE = 'cache'
OPT_MAPS = 'maps'
OPT_LOCKFILE = 'lockfile'
OPT_TIMESTAMP_DIR = 'timestamp_dir'
def __init__(self, env):
"""Initialize defaults for data we hold.
Args:
env: dictionary of environment variables (typically os.environ)
"""
# override constants based on ENV vars
if 'NSSCACHE_CONFIG' in env:
self.config_file = env['NSSCACHE_CONFIG']
else:
self.config_file = self.NSSCACHE_CONFIG
# default values
self.command = None
self.help_command = None
self.maps = []
self.options = {}
self.lockfile = None
self.timestamp_dir = None
self.log = logging.getLogger('config')
def __repr__(self):
"""String representation of this object."""
# self.options is of variable length so we are forced to do
# some fugly concatenation here to print our config in a
# readable fashion.
string = (('<Config:\n\tcommand=%r\n\thelp_command=%r\n\tmaps=%r'
'\n\tlockfile=%r\n\ttimestamp_dir=%s') %
(self.command, self.help_command, self.maps, self.lockfile,
self.timestamp_dir))
for key in self.options:
string = '%s\n\t%s=%r' % (string, key, self.options[key])
return '%s\n>' % string
class MapOptions(object):
"""Data container for individual maps.
Each map is configured against a source and cache. The
dictionaries used by the source and cache implementations are
stored here.
"""
def __init__(self):
"""Initialize default values."""
self.cache = {}
self.source = {}
def __repr__(self):
"""String representation of this object."""
return '<MapOptions cache=%r source=%r>' % (self.cache, self.source)
#
# Configuration itself is done through module-level methods. These
# methods are below.
#
def LoadConfig(configuration):
"""Load the on-disk configuration file and merge it into config.
Args:
configuration: a config.Config object
Raises:
error.NoConfigFound: no configuration file was found
"""
parser = ConfigParser.ConfigParser()
# load config file
configuration.log.debug('Attempting to parse configuration file: %s',
configuration.config_file)
parser.read(configuration.config_file)
# these are required, and used as defaults for each section
default = 'DEFAULT'
default_source = FixValue(parser.get(default, Config.OPT_SOURCE))
default_cache = FixValue(parser.get(default, Config.OPT_CACHE))
# this is also required, but global only
# TODO(v): make this default to /var/lib/nsscache before next release
configuration.timestamp_dir = FixValue(parser.get(default,
Config.OPT_TIMESTAMP_DIR))
# optional defaults
if parser.has_option(default, Config.OPT_LOCKFILE):
configuration.lockfile = FixValue(parser.get(default, Config.OPT_LOCKFILE))
if not configuration.maps:
# command line did not override
maplist = FixValue(parser.get(default, Config.OPT_MAPS))
# special case for empty string, or split(',') will return a
# non-empty list
if maplist:
configuration.maps = [m.strip() for m in maplist.split(',')]
else:
configuration.maps = []
# build per-map source and cache dictionaries and store
# them in MapOptions() objects.
for map_name in configuration.maps:
map_options = MapOptions()
source = default_source
cache = default_cache
# override source and cache if necessary
if parser.has_section(map_name):
if parser.has_option(map_name, Config.OPT_SOURCE):
source = FixValue(parser.get(map_name, Config.OPT_SOURCE))
if parser.has_option(map_name, Config.OPT_CACHE):
cache = FixValue(parser.get(map_name, Config.OPT_CACHE))
# load source and cache default options
map_options.source = Options(parser.items(default), source)
map_options.cache = Options(parser.items(default), cache)
# overide with any section-specific options
if parser.has_section(map_name):
options = Options(parser.items(map_name), source)
map_options.source.update(options)
options = Options(parser.items(map_name), cache)
map_options.cache.update(options)
# used to instantiate the specific cache/source
map_options.source['name'] = source
map_options.cache['name'] = cache
# save final MapOptions() in the parent config object
configuration.options[map_name] = map_options
configuration.log.info('Configured maps are: %s',
', '.join(configuration.maps))
configuration.log.debug('loaded configuration: %r', configuration)
def Options(items, name):
"""Returns a dict of options specific to an implementation.
This is used to retrieve a dict of options for a given
implementation. We look for configuration options in the form of
name_option and ignore the rest.
Args:
items: [('key1', 'value1'), ('key2, 'value2'), ...]
name: 'foo'
Returns:
dictionary of option:value pairs
"""
options = {}
option_re = re.compile('^%s_(.+)' % name)
for item in items:
match = option_re.match(item[0])
if match:
options[match.group(1)] = FixValue(item[1])
return options
def FixValue(value):
"""Helper function to fix values loaded from a config file.
Currently we strip bracketed quotes as well as convert numbers to
floats for configuration parameters expecting numerical data types.
Args:
value: value to be converted
Returns:
fixed value
"""
# Strip quotes if necessary.
if ((value.startswith('"') and value.endswith('"'))
or (value.startswith('\'') and value.endswith('\''))):
value = value[1:-1]
# Convert to float if necessary. Python converts between floats and ints
# on demand, but won't attempt string conversion automagically.
#
# Caveat: '1' becomes 1.0, however python treats it reliably as 1
# for native comparisons to int types, and if an int type is needed
# explicitly the caller will have to cast. This is simplist.
try:
value = int(value)
except ValueError:
try:
value = float(value)
except ValueError:
return value
return value
def ParseNSSwitchConf(nsswitch_filename):
"""Parse /etc/nsswitch.conf and return the sources for each map.
Args:
nsswitch_filename: Full path to an nsswitch.conf to parse. See manpage
nsswitch.conf(5) for full details on the format expected.
Returns:
a dictionary keyed by map names and containing a list of sources
for each map.
"""
nsswitch_file = open(nsswitch_filename, 'r')
nsswitch = {}
map_re = re.compile('^([a-z]+): *(.*)$')
for line in nsswitch_file:
match = map_re.match(line)
if match:
sources = match.group(2).split()
nsswitch[match.group(1)] = sources
return nsswitch
def VerifyConfiguration(conf, nsswitch_filename=FILE_NSSWITCH):
"""Verify that the system configuration matches the nsscache configuration.
Checks that NSS configuration has the cache listed for each map that
is configured in the nsscache configuration, i.e. that the system is
configured to use the maps we are building.
Args:
conf: a Configuration
nsswitch_filename: optionally the name of the file to parse
Returns:
(warnings, errors) a tuple counting the number of warnings and
errors detected
"""
(warnings, errors) = (0, 0)
if not conf.maps:
logging.error('No maps are configured.')
errors += 1
# Verify that at least one supported module is configured in nsswitch.conf.
nsswitch = ParseNSSwitchConf(nsswitch_filename)
for configured_map in conf.maps:
if configured_map == 'sshkey':
continue
if conf.options[configured_map].cache['name'] == 'nssdb':
nss_module_name = 'db'
if conf.options[configured_map].cache['name'] == 'files':
nss_module_name = 'files'
if ('cache_filename_suffix' in conf.options[configured_map].cache and
conf.options[configured_map].cache['cache_filename_suffix'] ==
'cache'):
# We are configured for libnss-cache for this map.
nss_module_name = 'cache'
else:
# TODO(jaq): default due to hysterical raisins
nss_module_name = 'db'
if nss_module_name not in nsswitch[configured_map]:
logging.warn(('nsscache is configured to build maps for %r, '
'but NSS is not configured (in %r) to use it'),
configured_map, nsswitch_filename)
warnings += 1
return (warnings, errors)
|