This file is indexed.

/usr/lib/python2.7/dist-packages/nss_cache/maps/maps.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
338
339
340
341
342
343
344
# 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.

"""Base class of maps for nsscache.

Map:  Abstract class representing a basic NSS map.
MapEntry:  Abstract class representing an entry in a NSS map.
"""

__author__ = 'vasilios@google.com (Vasilios Hoffman)'

import logging

from nss_cache import error


class Map(object):
  """Abstract class representing a basic NSS map.

  Map data is stored internally as a dict of MapEntry objects, with
  the key being the unique value provided by MapEntry.Key().

  MapEntry.Key() is implemented by returning the attribute value for
  some attribute which is expected to be unique, e.g. the name of a
  user or the name of a group.

  This allows for a fast implementation of __contains__() although
  it restricts Map objects from holding two MapEntry objects with
  the same keys (e.g. no two entries for root allowed).  This is
  considered an acceptable restriction as posix semantics imply that
  entries are unique in each map with respect to certain attributes.

  A Map also stores two timestamps; a "last update timestamp" which
  is set every time an update/merge operation occurs on a map, and a
  "last modification timestamp", which stores the last time that
  fresh data was merged into the map.

  N.B.  Changing the MapEntry().Key() after adding to a Map() will
  corrupt the index...so don't do it.

  Attributes:
    log: A logging.Logger instance used for output.
  """

  def __init__(self, iterable=None, modify_time=None, update_time=None):
    """Construct a Map object.

    Args:
      iterable: A tuple or list that can be iterated over and added to the Map,
        defaults to None.
      modify_time: An optional modify time for this Map, defaults to None.
        defaults to None.
      update_time: An optional update time for this Map, defaults to None.
         defaults to None.

    Raises:
      TypeError: If the objects in the iterable are of the wrong type.
    """
    if self.__class__ is Map:
      raise TypeError('Map is an abstract class.')
    self._data = {}
    # The index preserves the order that entries are returned from the source
    # (e.g. the LDAP server.)  It is not a set as sets are unordered.
    self._index = []
    self._last_modification_timestamp = modify_time
    self._last_update_timestamp = update_time

    self.log = logging.getLogger(self.__class__.__name__)

    # Seed with iterable, should raise TypeError for bad items.
    if iterable is not None:
      for item in iterable:
        self.Add(item)

  def __contains__(self, other):
    """Deep compare on a MapEntry."""
    key = other.Key()
    if key in self._data:
      possibility = self._data[key]
      if other == possibility:
        return True
    return False

  def __iter__(self):
    """Iterate over the MapEntry objects in this map.

    Actually this is a generator posing as an iterator so we can use the index
    to emit values in the original order.
    """
    for index_key in self._index:
      yield self._data[index_key]

  def __len__(self):
    """Returns the number of items in the map."""
    return len(self._data)

  def __repr__(self):
    return '<%s: %r>' % (self.__class__.__name__, self._data)

  def Add(self, entry):
    """Add a MapEntry object to the Map and verify it (overwrites).

    Args:
      entry: A maps.MapEntry instance.

    Returns:
      A boolean indicating the add is successful when True.

    Raises:
      TypeError: The object passed is not the right type.
    """

    # Correct type?
    if not isinstance(entry, MapEntry):
      raise TypeError('Not instance of MapEntry')

    # Entry okay?
    if not entry.Verify():
      self.log.info('refusing to add entry, verify failed')
      return False

    # Add to index if not already there.
    if not self._data.has_key(entry.Key()):
      self._index.append(entry.Key())
    else:
      self.log.warning('duplicate key detected when adding to map: %r, overwritten', entry.Key())
    
    self._data[entry.Key()] = entry
    return True

  def Exists(self, entry):
    """Deep comparison of a MapEntry to the MapEntry instances in the Map.

    Args:
      entry: A maps.MapEntry instance.

    Returns:
      A boolean indicating the object is present when True.
    """
    if entry in self:
      return True
    return False

  def Merge(self, other):
    """Update this Map based on another Map.

    Walk over other and for each entry, Add() it if it doesn't
    exist -- this will update changed entries as well as adding
    new ones.

    Args:
      other: A maps.Map instance.

    Returns:
      True if anything was added or modified, False if
      nothing changed.

    Raises:
      TypeError: Merging differently typed Maps.
      InvalidMerge: Attempt to Merge an older map into a newer one.
    """
    if type(self) != type(other):
      raise TypeError(
          'Attempt to Merge() differently typed Maps: %r != %r' %
          (type(self), type(other)))

    if other.GetModifyTimestamp() < self.GetModifyTimestamp():
      raise error.InvalidMerge(
          'Attempt to Merge a map with an older modify time into a newer one: '
          'other: %s, self: %s' %
          (other.GetModifyTimestamp(), self.GetModifyTimestamp()))

    if other.GetUpdateTimestamp() < self.GetUpdateTimestamp():
      raise error.InvalidMerge(
          'Attempt to Merge a map with an older update time into a newer one: '
          'other: %s, self: %s' %
          (other.GetUpdateTimestamp(), self.GetUpdateTimestamp()))

    self.log.info('merging from a map of %d entries', len(other))

    merge_count = 0
    for their_entry in other:
      if their_entry not in self:
        # Add() will overwrite similar entries if they exist.
        if self.Add(their_entry):
          merge_count += 1

    self.log.info('%d of %d entries were new or modified',
                  merge_count, len(other))

    if merge_count > 0:
      self.SetModifyTimestamp(other.GetModifyTimestamp())

    # set last update timestamp
    self.SetUpdateTimestamp(other.GetUpdateTimestamp())

    return merge_count > 0

  def PopItem(self):
    """Return a MapEntry object, throw KeyError if none exist.

    Returns:
      A maps.MapEntry from within maps.Map internal dict.

    Raises:
      KeyError if there is nothing to return
    """
    try:
      # pop items off the start of the index, in sorted order.
      index_key = self._index.pop(0)
    except IndexError:
      raise KeyError # Callers expect a KeyError rather than IndexError
    return self._data.pop(index_key) # Throws the KeyError if empty.

  def SetModifyTimestamp(self, value):
    """Set the last modify timestamp of this map.

    Args:
      value: An integer containing the number of seconds since epoch, or None.

    Raises:
      TypeError: The argument is not an int or None.
    """
    if value is None or isinstance(value, int):
      self._last_modification_timestamp = value
    else:
      raise TypeError('timestamp can only be int or None, not %r'
                      % value)

  def GetModifyTimestamp(self):
    """Return last modification timestamp of this map.

    Returns:
      Either an int containing seconds since epoch, or None.
    """
    return self._last_modification_timestamp

  def SetUpdateTimestamp(self, value):
    """Set the last update timestamp of this map.

    Args:
      value:  An int containing seconds since epoch, or None.

    Raises:
      TypeError: The argument is not an int or None.
    """
    if value is None or isinstance(value, int):
      self._last_update_timestamp = value
    else:
      raise TypeError('timestamp can only be int or None, not %r',
                      value)

  def GetUpdateTimestamp(self):
    """Return last update timestamp of this map.

    Returns:
      An int containing seconds since epoch, or None.
    """
    return self._last_update_timestamp


class MapEntry(object):
  """Abstract class for representing an entry in an NSS map.

  We expect to be contained in MapEntry objects and provide a unique identifier
  via Key() so that Map objects can properly index us.  See the Map class for
  more details.

  Attributes:
    log: A logging.Logger instance used for output.
  """
  # Using slots saves us over 2x memory on large maps.
  __slots__ = ('_KEY', '_ATTRS', 'log')
  # Overridden in the derived classes
  _KEY = None
  _ATTRS = None

  def __init__(self, data=None):
    """This is an abstract class.

    Args:
      data:  An optional dict of attribute, value pairs to populate with.

    Raises:
      TypeError:  Bad argument, or attempt to instantiate abstract class.
    """
    if self.__class__ is MapEntry:
      raise TypeError('MapEntry is an abstract class.')

    # Initialize from dict, if passed.
    if data is None:
      return
    else:
      for key in data:
        setattr(self, key, data[key])

    self.log = logging.getLogger(self.__class__.__name__)

  def __eq__(self, other):
    """Deep comparison of two MapEntry objects."""
    if type(self) != type(other):
      return False
    for key in self._ATTRS:
      if getattr(self, key) != getattr(other, key, None):
        return False
    return True

  def __repr__(self):
    """String representation."""
    rep = ''
    for key in self._ATTRS:
      rep = '%r:%r %s' % (key, getattr(self, key), rep)
    return '<%s : %r>' % (self.__class__.__name__, rep.rstrip())

  def Key(self):
    """Return unique identifier for this MapEntry object.

    Returns:
      A str which contains the name of the attribute to be used as an index
      value for a maps.MapEntry instance in a maps.Map.
    """
    return getattr(self, self._KEY)

  def Verify(self):
    """We can properly index this instance into a Map.

    Returns:
      True if the value in the attribute named by self._KEY for this class
      is not None.  False otherwise.
    """
    return getattr(self, self._KEY) is not None