/* * Copyright (C) 2012-2013 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: Jan Synacek */ #include "file.h" CMPIStatus lmi_check_required( const CMPIBroker *b, const CMPIObjectPath *o, const enum RequiredNames rn) { const char *prop; const char *errmsg = NULL; char *path = NULL; /* check computer system creation class name */ if (CMIsNullValue(CMGetKey(o, "CSCreationClassName", NULL))) { errmsg = "CSCreationClassName is empty"; goto done; } prop = get_string_property_from_op(o, "CSCreationClassName"); if (strcmp(prop, lmi_get_system_creation_class_name())) { errmsg = "Wrong CSCreationClassName"; goto done; } /* check fqdn */ if (CMIsNullValue(CMGetKey(o, "CSName", NULL))) { errmsg = "CSName is empty"; goto done; } prop = get_string_property_from_op(o, "CSName"); if (strcmp(prop, lmi_get_system_name())) { errmsg = "Wrong CSName"; goto done; } if (rn == UNIXFILE) { /* check creation class name */ char fileclass[BUFLEN]; if (CMIsNullValue(CMGetKey(o, "LFCreationClassName", NULL))) { errmsg = "LFCreationClassName is empty"; goto done; } prop = get_string_property_from_op(o, "LFCreationClassName"); if (get_class_from_path(get_string_property_from_op(o, "LFName"), fileclass) != 0) { errmsg = "Can't get class from path"; goto done; } if (strcmp(prop, fileclass)) { errmsg = "LFCreationClassName doesn't match the file's type"; goto done; } if (CMIsNullValue(CMGetKey(o, "LFName", NULL))) { errmsg = "LFName is empty"; goto done; } if (get_fsname_from_path(get_string_property_from_op(o, "LFName"), &path) < 0) { errmsg = "Can't get FSName from path"; goto done; } } else if (rn == LOGICALFILE) { /* check creation class name */ if (CMIsNullValue(CMGetKey(o, "CreationClassName", NULL))) { errmsg = "CreationClassName is empty"; goto done; } prop = get_string_property_from_op(o, "CreationClassName"); if (!CMClassPathIsA(b, o, prop, NULL)) { errmsg = "CreationClassName and the class name don't match"; goto done; } if (CMIsNullValue(CMGetKey(o, "Name", NULL))) { errmsg = "Name is empty"; goto done; } if (get_fsname_from_path(get_string_property_from_op(o, "Name"), &path) < 0) { errmsg = "Can't get FSName from path"; goto done; } } else { /* not possible! */ assert(0); } /* check fs creation class name and fsname */ if (CMIsNullValue(CMGetKey(o, "FSCreationClassName", NULL))) { errmsg = "FSCreationClassName is empty"; goto done; } prop = get_string_property_from_op(o, "FSCreationClassName"); if (strcmp(prop, FSCREATIONCLASSNAME)) { errmsg = "Wrong FSCreationClassName"; goto done; } if (CMIsNullValue(CMGetKey(o, "FSName", NULL))) { errmsg = "FSName is empty"; goto done; } prop = get_string_property_from_op(o, "FSName"); if (strcmp(prop, path)) { errmsg = "Wrong FSName"; goto done; } done: if (path) { free(path); } if (errmsg) { CMReturnWithChars(b, CMPI_RC_ERR_FAILED, errmsg); } CMReturn(CMPI_RC_OK); } void get_class_from_stat(const struct stat *sb, char *fileclass) { (S_ISREG(sb->st_mode)) ? strcpy(fileclass, "LMI_DataFile") : (S_ISDIR(sb->st_mode)) ? strcpy(fileclass, "LMI_UnixDirectory") : (S_ISCHR(sb->st_mode)) ? strcpy(fileclass, "LMI_UnixDeviceFile") : (S_ISBLK(sb->st_mode)) ? strcpy(fileclass, "LMI_UnixDeviceFile") : (S_ISLNK(sb->st_mode)) ? strcpy(fileclass, "LMI_SymbolicLink") : (S_ISFIFO(sb->st_mode)) ? strcpy(fileclass, "LMI_FIFOPipeFile") : (S_ISSOCK(sb->st_mode)) ? strcpy(fileclass, "LMI_UnixSocket") : strcpy(fileclass, "Unknown"); assert(strcmp(fileclass, "Unknown") != 0); } int get_class_from_path(const char *path, char *fileclass) { int rc = 0; struct stat sb; if (lstat(path, &sb) < 0) { rc = 1; } else { get_class_from_stat(&sb, fileclass); } return rc; } int get_fsname_from_stat(const struct stat *sb, char **fname) { static struct udev *udev_ctx = NULL; struct udev_device *udev_dev; const char *dev_name; int rc = 0; if (!udev_ctx) { udev_ctx = udev_new(); if (!udev_ctx) { rc = -1; goto err; } } char dev_id[16]; snprintf(dev_id, 16, "b%u:%u", major(sb->st_dev), minor(sb->st_dev)); udev_dev = udev_device_new_from_device_id(udev_ctx, dev_id); if ((dev_name = udev_device_get_property_value(udev_dev, "ID_FS_UUID_ENC"))) { rc = asprintf(fname, "UUID=%s", dev_name); } else if ((dev_name = udev_device_get_property_value(udev_dev, "DEVNAME"))) { rc = asprintf(fname, "DEVICE=%s", dev_name); } else { rc = asprintf(fname, "Unknown"); } err: return rc; } int get_fsname_from_path(const char *path, char **fsname) { struct stat sb; int rc; rc = lstat(path, &sb); if (rc == 0) { rc = get_fsname_from_stat(&sb, fsname); } return rc; } const char *get_string_property_from_op(const CMPIObjectPath *o, const char *prop) { CMPIData d; d = CMGetKey(o, prop, NULL); return KChars(d.value.string); } void _dump_objectpath(const CMPIObjectPath *o) { printf("OP: %s\n", CMGetCharsPtr(o->ft->toString(o, NULL), NULL)); } /* vi: set et: */