summaryrefslogtreecommitdiffstats
path: root/src/software/openlmi/software/core/SoftwareFileCheck.py
blob: 409db09d70d301a34299011d799cf970bf6d3550 (plain)
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# -*- encoding: utf-8 -*-
# Software Management Providers
#
# Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# Authors: Michal Minar <miminar@redhat.com>
#

"""
Just a common functionality related to SoftwareFileCheck provider.
"""

import collections
import hashlib
import os
import pywbem
import stat
import yum

from openlmi.common import cmpi_logging
from openlmi.software import util
from openlmi.software.yumdb import YumDB
from openlmi.software.yumdb import packageinfo
from openlmi.software.yumdb import packagecheck

PASSED_FLAGS_DESCRIPTIONS = (
        "Existence",
        "File Type",
        "File Size",
        "File Mode",
        "File Checksum",
        "Device major/minor number",
        "Symlink Target",
        "User Ownership", "Group Ownership",
        "Modify Time")

# Named tuple to store results of rpm file check as pywbem values, all results
# are in the form:
#   (expected, reality)
# where
#   expected is value from rpm package
#   reality  is value obtained from installed file
# None means, that value could not be obtained. Except for "exists" and
# "md5_checksum" attributes, where "exists" is boolean and "md5_checksum" is
# a string.
# for example:
#   file_check.file_type == (4, 3)
FileCheck = collections.namedtuple('FileCheck',     #pylint: disable=C0103
        'exists, md5_checksum, file_type, file_size, file_mode, '
        'file_checksum, device, link_target, user_id, group_id, '
        'last_modification_time')

@cmpi_logging.trace_function
def checksumtype_num2hash(csumt):
    """
    @param csumt checksum type as a number obtained from package
    @return hash function object corresponding to csumt
    """
    return getattr(hashlib, yum.constants.RPM_CHECKSUM_TYPES[csumt])

@cmpi_logging.trace_function
def checksumtype_str2pywbem(alg):
    """
    @param alg is a name of algorithm used for checksum
    @return pywbem number corresponding to given alg
    """
    try:
        res = packagecheck.CHECKSUMTYPE_STR2NUM[alg.lower()]
    except KeyError:
        res = 0
    return pywbem.Uint16(res)

@cmpi_logging.trace_function
def filetype_str2pywbem(file_type):
    """
    @param file_type is a name of file type obtained from pkg headers
    @return pywbem number corresponding to thus file type
    """
    try:
        return pywbem.Uint16(
                { 'file'             : Values.FileType.File
                , 'directory'        : Values.FileType.Directory
                , 'symlink'          : Values.FileType.Symlink
                , 'fifo'             : Values.FileType.FIFO
                , 'character device' : Values.FileType.Character_Device
                , 'block device'     : Values.FileType.Block_Device
                }[file_type])
    except KeyError:
        return Values.FileType.Unknown

@cmpi_logging.trace_function
def filetype_mode2pywbem(mode):
    """
    @param mode is a raw file mode as integer
    @return pywbem numeric value of file's type
    """
    for i, name in enumerate(
            ('REG', 'DIR', 'LNK', 'FIFO', 'CHR', 'BLK'), 1):
        if getattr(stat, 'S_IS' + name)(mode): 
            return pywbem.Uint16(i)
    return pywbem.Uint16(0)

@cmpi_logging.trace_function
def mode2pywbem_flags(mode):
    """
    @param mode if None, file does not exist
    @return list of integer flags describing file's access permissions
    """
    if mode is None:
        return None
    flags = []
    for i, flag in enumerate((
            stat.S_IXOTH,
            stat.S_IWOTH,
            stat.S_IROTH,
            stat.S_IXGRP,
            stat.S_IWGRP,
            stat.S_IRGRP,
            stat.S_IXUSR,
            stat.S_IWUSR,
            stat.S_IRUSR,
            stat.S_ISVTX,
            stat.S_ISGID,
            stat.S_ISUID)):
        if flag & mode:
            flags.append(pywbem.Uint8(i))
    return flags

@cmpi_logging.trace_function
def hashfile(afile, hashers, blocksize=65536):
    """
    @param hashers is a list of hash objects
    @return list of digest strings (in hex format) for each hash object
    given in the same order
    """
    if not isinstance(hashers, (tuple, list, set, frozenset)):
        hashers = (hashers, )
    buf = afile.read(blocksize)
    while len(buf) > 0:
        for hashfunc in hashers:
            hashfunc.update(buf)
        buf = afile.read(blocksize)
    return [ hashfunc.hexdigest() for hashfunc in hashers ]

@cmpi_logging.trace_function
def compute_checksums(checksum_type, file_type, file_path):
    """
    @param file_type is not a file, then zeroes are returned
    @param checksum_type selected hash algorithm to compute second
    checksum
    @return (md5sum, checksum)
    both checksums are computed from file_path's content
    first one is always md5, the second one depends on checksum_type
    if file does not exists, (None, None) is returned
    """
    hashers = [hashlib.md5()]   #pylint: disable=E1101
    if checksum_type != packagecheck.CHECKSUMTYPE_STR2NUM["md5"]:
        hashers.append(checksumtype_num2hash(checksum_type)())
    if file_type != filetype_str2pywbem('file'):
        rslts = ['0'*len(h.hexdigest()) for h in hashers]
    else:
        try:
            with open(file_path, 'rb') as fobj:
                rslts = hashfile(fobj, hashers)
        except (OSError, IOError) as exc:
            cmpi_logging.logger.error("could not open file \"%s\""
                    " for reading: %s", file_path, exc)
            return None, None
    return (rslts[0], rslts[1] if len(rslts) > 1 else rslts[0]*2)

@cmpi_logging.trace_function
def object_path2pkg_file(objpath):
    """
    @return (package_info, package_check)
    """
    if not isinstance(objpath, pywbem.CIMInstanceName):
        raise TypeError("objpath must be instance of CIMInstanceName, "
                "not \"%s\"" % objpath.__class__.__name__)

    if (  not objpath['Name'] or not objpath['SoftwareElementID']
       or not objpath['CheckID']
       or not objpath['CheckID'].endswith('#'+objpath['Name'])
       or objpath['SoftwareElementID'].find(objpath['Version']) == -1):
        raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND, "Wrong keys.")
    if objpath['SoftwareElementState'] not in ("2", 2):
        raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND,
                "Only \"Executable\" software element state supported")
    if not util.check_target_operating_system(objpath['TargetOperatingSystem']):
        raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND,
                "Wrong target operating system.")
    if not objpath['Name'] or not objpath['Version']:
        raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND,
                'Both "Name" and "Version" must be given')
    match = util.RE_NEVRA_OPT_EPOCH.match(objpath['SoftwareElementID'])
    if not match:
        raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND,
                "Wrong SotwareElementID. Expected valid nevra"
                " (name-epoch:version-release.arch).")
    if objpath['Version'] != match.group('ver'):
        raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND,
            "Version does not match version part in SoftwareElementID.")

    with YumDB.getInstance() as ydb:
        pkglist = ydb.filter_packages('installed', **util.nevra2filter(match))
        if len(pkglist) < 1:
            raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND,
                    "No matching package installed.")
        pkg = pkglist[0]
        pkg_check = ydb.check_package(pkg)
        return (pkg, pkg_check, pkg_check[objpath["Name"]])

@cmpi_logging.trace_function
def test_file(checksum_type, package_file):
    """
    @param checksum type is a pywbem value for ChecksumType property
    @return instance of FileCheck
    """
    if not isinstance(package_file, packagecheck.PackageFile):
        raise TypeError("package_file must be an instance of PackageFile"
        " not \"%s\"" % package_file.__class__.__name__)
    exists = os.path.lexists(package_file.path)
    md5_checksum = None
    expected = {
        "file_type"     : filetype_str2pywbem(package_file.file_type),
        "user_id"       : pywbem.Uint32(package_file.uid),
        "group_id"      : pywbem.Uint32(package_file.gid),
        "file_mode"     : pywbem.Uint32(package_file.mode),
        "file_size"     : pywbem.Uint64(package_file.size),
        "link_target"   : package_file.link_target,
        "file_checksum" : package_file.checksum,
        "device"        : pywbem.Uint64(package_file.device)
            if package_file.device is not None else None,
        "last_modification_time" : pywbem.Uint64(package_file.mtime)
    }
    if not exists:
        reality = collections.defaultdict(lambda: None)
    else:
        fstat = os.lstat(package_file.path)
        reality = {
            "file_type" : filetype_mode2pywbem(fstat.st_mode),
            "user_id"   : pywbem.Uint32(fstat.st_uid),
            "group_id"  : pywbem.Uint32(fstat.st_gid),
            "file_mode" : pywbem.Uint32(fstat.st_mode),
            "file_size" : pywbem.Uint64(fstat.st_size),
            "last_modification_time" : pywbem.Uint64(fstat.st_mtime)
        }
        reality["device"] = (
                 pywbem.Uint64(fstat.st_dev)
            if   reality['file_type'] == filetype_str2pywbem("device")
            else None)
        reality["link_target"] = (os.readlink(package_file.path)
                 if os.path.islink(package_file.path) else None)
        md5_checksum, checksum = compute_checksums(
                checksum_type, reality["file_type"], package_file.path)
        reality["file_checksum"] = checksum
    kwargs = dict(exists=exists, md5_checksum=md5_checksum,
            **dict((k, (expected[k], reality[k])) for k in expected))
    return FileCheck(**kwargs)

@cmpi_logging.trace_function
def _filecheck2model_flags(file_check):
    """
    @param file_check is an instance of FileCheck
    @return pywbem value for PassedFlags property
    """
    if not isinstance(file_check, FileCheck):
        raise TypeError("file_check must be an instance of FileCheck")
    flags = []
    for k, value in file_check._asdict().items(): #pylint: disable=W0212
        if isinstance(value, tuple):
            if (   k in ("last_modification_time", "file_size")
               and file_check.file_type[0] != filetype_str2pywbem('file')):
                # last_modification_time check is valid only for
                # regular files
                flag = file_check.exists
            elif (   k == "file_mode"
                 and file_check.file_type[0] == filetype_str2pywbem('symlink')):
                # do not check mode of symlinks
                flag = (   file_check.exists
                       and file_check.file_type[0] == file_check.file_type[1])
            else:
                flag = file_check.exists and value[0] == value[1]
            flags.append(flag)
        elif isinstance(value, bool):
            flags.append(value)
    return flags

@cmpi_logging.trace_function
def filecheck_passed(file_check):
    """
    @return True if installed file passed all checks.
    """
    return all(_filecheck2model_flags(file_check))

@cmpi_logging.trace_function
def _fill_non_key_values(model, pkg_check, pkg_file, file_check=None):
    """
    Fills a non key values into instance of SoftwareFileCheck.
    """
    model['FileName'] = os.path.basename(pkg_file.path)
    model['FileChecksumType'] = csumt = pywbem.Uint16(
            pkg_check.file_checksum_type)
    if file_check is None:
        file_check = test_file(csumt, pkg_file)
    for mattr, fattr in (
            ('FileType', 'file_type'),
            ('FileUserID', 'user_id'),
            ('FileGroupID', 'group_id'),
            ('FileMode', 'file_mode'),
            ('LastModificationTime', 'last_modification_time'),
            ('FileSize', 'file_size'),
            ('LinkTarget', 'link_target'),
            ('FileChecksum', 'file_checksum')):
        exp, rea = getattr(file_check, fattr)
        if exp is not None:
            model['Expected' + mattr] = exp
        if rea is not None:
            model[mattr] = rea
    model['ExpectedFileModeFlags'] = mode2pywbem_flags(file_check.file_mode[0])
    if file_check.exists:
        model['FileModeFlags'] = mode2pywbem_flags(file_check.file_mode[1])
    model['FileExists'] = file_check.exists
    if file_check.md5_checksum is not None:
        model['MD5Checksum'] = file_check.md5_checksum
    model['PassedFlags'] = _filecheck2model_flags(file_check)
    model['PassedFlagsDescriptions'] = list(PASSED_FLAGS_DESCRIPTIONS)

@cmpi_logging.trace_function
def filecheck2model(package_info, package_check, file_name, keys_only=True,
        model=None, file_check=None):
    """
    @param package_file is an instance of yumdb.PackageFile
    @param file_name a absolute file path contained in package
    @param keys_only if True, then only key values will be filed
    @param model if given, then this instance will be modified and
    returned
    @param file_check if not given, it will be computed
    @return instance of LMI_SoftwareFileCheck class with all desired
    values filed
    """
    if not isinstance(package_info, packageinfo.PackageInfo):
        raise TypeError(
                "package_info must be an instance ofyumdb.PackageInfo")
    if not isinstance(package_check, packagecheck.PackageCheck):
        raise TypeError(
                "package_check must be an instance of yumdb.PackageFile")
    if not file_name in package_check:
        raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND,
                "File \"%s\" not found among package files" % file_name)
    if model is None:
        model = pywbem.CIMInstanceName("LMI_SoftwareFileCheck",
                namespace="root/cimv2")
        if not keys_only:
            model = pywbem.CIMInstance("LMI_SoftwareFileCheck", path=model)
    package_file = package_check[file_name]
    model['Name'] = package_file.path
    model['SoftwareElementID'] = package_info.nevra
    model['SoftwareElementState'] = Values.SoftwareElementState.Executable
    model['TargetOperatingSystem'] = pywbem.Uint16(
            util.get_target_operating_system()[0])
    model['Version'] = package_info.version
    model['CheckID'] = '%s#%s' % (package_info.name, package_file.path)
    if not keys_only:
        if file_check is not None:
            if not isinstance(file_check, FileCheck):
                raise TypeError("file_check must be an instance of FileCheck")
        _fill_non_key_values(model, package_check, package_file, file_check)
    return model

class Values(object):
    """
    Enumerations of LMI_SoftwareFileCheck class properties.
    """
    class TargetOperatingSystem(object):
        Unknown = pywbem.Uint16(0)
        Other = pywbem.Uint16(1)
        MACOS = pywbem.Uint16(2)
        ATTUNIX = pywbem.Uint16(3)
        DGUX = pywbem.Uint16(4)
        DECNT = pywbem.Uint16(5)
        Tru64_UNIX = pywbem.Uint16(6)
        OpenVMS = pywbem.Uint16(7)
        HPUX = pywbem.Uint16(8)
        AIX = pywbem.Uint16(9)
        MVS = pywbem.Uint16(10)
        OS400 = pywbem.Uint16(11)
        OS_2 = pywbem.Uint16(12)
        JavaVM = pywbem.Uint16(13)
        MSDOS = pywbem.Uint16(14)
        WIN3x = pywbem.Uint16(15)
        WIN95 = pywbem.Uint16(16)
        WIN98 = pywbem.Uint16(17)
        WINNT = pywbem.Uint16(18)
        WINCE = pywbem.Uint16(19)
        NCR3000 = pywbem.Uint16(20)
        NetWare = pywbem.Uint16(21)
        OSF = pywbem.Uint16(22)
        DC_OS = pywbem.Uint16(23)
        Reliant_UNIX = pywbem.Uint16(24)
        SCO_UnixWare = pywbem.Uint16(25)
        SCO_OpenServer = pywbem.Uint16(26)
        Sequent = pywbem.Uint16(27)
        IRIX = pywbem.Uint16(28)
        Solaris = pywbem.Uint16(29)
        SunOS = pywbem.Uint16(30)
        U6000 = pywbem.Uint16(31)
        ASERIES = pywbem.Uint16(32)
        HP_NonStop_OS = pywbem.Uint16(33)
        HP_NonStop_OSS = pywbem.Uint16(34)
        BS2000 = pywbem.Uint16(35)
        LINUX = pywbem.Uint16(36)
        Lynx = pywbem.Uint16(37)
        XENIX = pywbem.Uint16(38)
        VM = pywbem.Uint16(39)
        Interactive_UNIX = pywbem.Uint16(40)
        BSDUNIX = pywbem.Uint16(41)
        FreeBSD = pywbem.Uint16(42)
        NetBSD = pywbem.Uint16(43)
        GNU_Hurd = pywbem.Uint16(44)
        OS9 = pywbem.Uint16(45)
        MACH_Kernel = pywbem.Uint16(46)
        Inferno = pywbem.Uint16(47)
        QNX = pywbem.Uint16(48)
        EPOC = pywbem.Uint16(49)
        IxWorks = pywbem.Uint16(50)
        VxWorks = pywbem.Uint16(51)
        MiNT = pywbem.Uint16(52)
        BeOS = pywbem.Uint16(53)
        HP_MPE = pywbem.Uint16(54)
        NextStep = pywbem.Uint16(55)
        PalmPilot = pywbem.Uint16(56)
        Rhapsody = pywbem.Uint16(57)
        Windows_2000 = pywbem.Uint16(58)
        Dedicated = pywbem.Uint16(59)
        OS_390 = pywbem.Uint16(60)
        VSE = pywbem.Uint16(61)
        TPF = pywbem.Uint16(62)
        Windows__R__Me = pywbem.Uint16(63)
        Caldera_Open_UNIX = pywbem.Uint16(64)
        OpenBSD = pywbem.Uint16(65)
        Not_Applicable = pywbem.Uint16(66)
        Windows_XP = pywbem.Uint16(67)
        z_OS = pywbem.Uint16(68)
        Microsoft_Windows_Server_2003 = pywbem.Uint16(69)
        Microsoft_Windows_Server_2003_64_Bit = pywbem.Uint16(70)
        Windows_XP_64_Bit = pywbem.Uint16(71)
        Windows_XP_Embedded = pywbem.Uint16(72)
        Windows_Vista = pywbem.Uint16(73)
        Windows_Vista_64_Bit = pywbem.Uint16(74)
        Windows_Embedded_for_Point_of_Service = pywbem.Uint16(75)
        Microsoft_Windows_Server_2008 = pywbem.Uint16(76)
        Microsoft_Windows_Server_2008_64_Bit = pywbem.Uint16(77)
        FreeBSD_64_Bit = pywbem.Uint16(78)
        RedHat_Enterprise_Linux = pywbem.Uint16(79)
        RedHat_Enterprise_Linux_64_Bit = pywbem.Uint16(80)
        Solaris_64_Bit = pywbem.Uint16(81)
        SUSE = pywbem.Uint16(82)
        SUSE_64_Bit = pywbem.Uint16(83)
        SLES = pywbem.Uint16(84)
        SLES_64_Bit = pywbem.Uint16(85)
        Novell_OES = pywbem.Uint16(86)
        Novell_Linux_Desktop = pywbem.Uint16(87)
        Sun_Java_Desktop_System = pywbem.Uint16(88)
        Mandriva = pywbem.Uint16(89)
        Mandriva_64_Bit = pywbem.Uint16(90)
        TurboLinux = pywbem.Uint16(91)
        TurboLinux_64_Bit = pywbem.Uint16(92)
        Ubuntu = pywbem.Uint16(93)
        Ubuntu_64_Bit = pywbem.Uint16(94)
        Debian = pywbem.Uint16(95)
        Debian_64_Bit = pywbem.Uint16(96)
        Linux_2_4_x = pywbem.Uint16(97)
        Linux_2_4_x_64_Bit = pywbem.Uint16(98)
        Linux_2_6_x = pywbem.Uint16(99)
        Linux_2_6_x_64_Bit = pywbem.Uint16(100)
        Linux_64_Bit = pywbem.Uint16(101)
        Other_64_Bit = pywbem.Uint16(102)
        Microsoft_Windows_Server_2008_R2 = pywbem.Uint16(103)
        VMware_ESXi = pywbem.Uint16(104)
        Microsoft_Windows_7 = pywbem.Uint16(105)
        CentOS_32_bit = pywbem.Uint16(106)
        CentOS_64_bit = pywbem.Uint16(107)
        Oracle_Enterprise_Linux_32_bit = pywbem.Uint16(108)
        Oracle_Enterprise_Linux_64_bit = pywbem.Uint16(109)
        eComStation_32_bitx = pywbem.Uint16(110)

    class SoftwareElementState(object):
        Deployable = pywbem.Uint16(0)
        Installable = pywbem.Uint16(1)
        Executable = pywbem.Uint16(2)
        Running = pywbem.Uint16(3)

    class FileType(object):
        Unknown = pywbem.Uint16(0)
        File = pywbem.Uint16(1)
        Directory = pywbem.Uint16(2)
        Symlink = pywbem.Uint16(3)
        FIFO = pywbem.Uint16(4)
        Character_Device = pywbem.Uint16(5)
        Block_Device = pywbem.Uint16(6)