def __init__(self):
record_size_32 = struct.calcsize(self.LASTLOG_STRUCT_32)
record_size_64 = struct.calcsize(self.LASTLOG_STRUCT_64)
- if platform.architecture()[0] == "32bit":
- self.LASTLOG_STRUCT = self.LASTLOG_STRUCT_32
- record_size = record_size_32
- elif platform.architecture()[0] == "64bit":
+ # some 64bit arches have 32bit-compatible lastlog structures, others don't,
+ # in apparently incoherent ways, so hardcode a list...
+ if platform.machine() in ('ia64', 'aarch64', 's390x'):
self.LASTLOG_STRUCT = self.LASTLOG_STRUCT_64
record_size = record_size_64
else:
- raise RuntimeError('Unknown architecture, cannot interpret platform.architecture()[0] value (%d)' % platform.architecture()[0])
+ self.LASTLOG_STRUCT = self.LASTLOG_STRUCT_32
+ record_size = record_size_32
with open('/var/log/lastlog', 'r') as fp:
uid = -1 # there is one record per uid in lastlog
for record in iter(lambda: fp.read(record_size), ''):
+ if len(record) != record_size:
+ raise RuntimeError('lastlog has unexpected size, read %d instead of %d'
+ % (len(record), record_size))
uid += 1 # so keep incrementing uid for each record read
lastlog_time, _, _ = list(struct.unpack(self.LASTLOG_STRUCT, record))
+ if lastlog_time < 0:
+ raise RuntimeError('unexpected last login time %d for user %s'
+ % (lastlog_time, pwd.getpwuid(uid).pw_name))
try:
self[pwd.getpwuid(uid).pw_name] = lastlog_time
except KeyError: