int run_edit (const char *cmd, size_t argc, char *argv[]) { const char *editor; CLEANUP_FREE char *remotefilename = NULL; int r; if (argc != 1) { fprintf (stderr, _("use '%s filename' to edit a file\n"), cmd); return -1; } /* Choose an editor. */ if (STRCASEEQ (cmd, "vi")) editor = "vi"; else if (STRCASEEQ (cmd, "emacs")) editor = "emacs -nw"; else editor = NULL; /* use $EDITOR */ /* Handle 'win:...' prefix. */ remotefilename = win_prefix (argv[0]); if (remotefilename == NULL) return -1; r = edit_file_editor (g, remotefilename, editor, NULL, 0 /* not verbose */); return r == -1 ? -1 : 0; }
static void _iodbcdm_env_settracing (GENV_t *genv) { char buf[1024]; genv = genv; /*UNUSED*/ /* * Check TraceFile keyword */ SQLSetConfigMode (ODBC_BOTH_DSN); if( SQLGetPrivateProfileString ("ODBC", "TraceFile", "", buf, sizeof(buf) / sizeof(SQLTCHAR), "odbc.ini") == 0 || !buf[0]) STRCPY (buf, SQL_OPT_TRACE_FILE_DEFAULT); trace_set_filename (buf); /* * Check Trace keyword */ SQLSetConfigMode (ODBC_BOTH_DSN); if ( SQLGetPrivateProfileString ("ODBC", "Trace", "", buf, sizeof(buf) / sizeof(SQLTCHAR), "odbc.ini") && (STRCASEEQ (buf, "on") || STRCASEEQ (buf, "yes") || STRCASEEQ (buf, "1"))) trace_start (); return; }
/** * <user_agent> */ static void s_set_user_agent_data(Doc *doc, apr_pool_t *p, mod_chxj_config *conf, Node *node) { Node *child; device_table_list *t; for (child = qs_get_child_node(doc,node); child ; child = qs_get_next_node(doc,child)) { char *name = qs_get_node_name(doc,child); if (STRCASEEQ('u','U',"user_agent",name)) { Attr *attr; device_table_list *dtl; if (! conf->devices) { conf->devices = apr_pcalloc(p, sizeof(device_table_list)); conf->devices->next = NULL; conf->devices->pattern = NULL; conf->devices->table = NULL; conf->devices->tail = NULL; dtl = conf->devices; } else { for (t = conf->devices; t ; t = t->next) { if (! t->next) break; } t->next = apr_pcalloc(p, sizeof(device_table_list)); t->next->next = NULL; t->next->pattern = NULL; t->next->table = NULL; t->next->tail = NULL; dtl = t->next; } for (attr = qs_get_attr(doc,child); attr ; attr = qs_get_next_attr(doc,attr)) { char *attr_name = qs_get_attr_name(doc,attr); if (STRCASEEQ('p','P',"pattern",attr_name)) { dtl->pattern = apr_pstrdup(p, qs_get_attr_value(doc,attr)); dtl->regexp = ap_pregcomp(p, (const char *)dtl->pattern, AP_REG_EXTENDED|AP_REG_ICASE); } } s_set_device_data(doc, p, dtl, child); } } }
int xenapiUtil_ParseQuery(virConnectPtr conn, virURIPtr uri, int *noVerify) { int result = 0; size_t i; for (i = 0; i < uri->paramsCount; i++) { virURIParamPtr queryParam = &uri->params[i]; if (STRCASEEQ(queryParam->name, "no_verify")) { if (noVerify == NULL) { continue; } if (virStrToLong_i(queryParam->value, NULL, 10, noVerify) < 0 || (*noVerify != 0 && *noVerify != 1)) { xenapiSessionErrorHandler(conn, VIR_ERR_INVALID_ARG, _("Query parameter 'no_verify' has unexpected value (should be 0 or 1)")); goto failure; } } } cleanup: return result; failure: result = -1; goto cleanup; }
int is_utf8_encoding (const char *encoding) { if (STRCASEEQ (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0)) return 1; return 0; }
/* Very inefficient, but at least having a separate API call * allows us to make it more efficient in future. */ hive_value_h hivex_node_get_value (hive_h *h, hive_node_h node, const char *key) { hive_value_h *values = NULL; char *name = NULL; hive_value_h ret = 0; values = hivex_node_values (h, node); if (!values) goto error; size_t i; for (i = 0; values[i] != 0; ++i) { name = hivex_value_key (h, values[i]); if (!name) goto error; if (STRCASEEQ (name, key)) { ret = values[i]; break; } free (name); name = NULL; } error: free (values); free (name); return ret; }
static int openvzGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED, const char *type) { if (type == NULL || STRCASEEQ(type, "openvz")) return 1028; /* OpenVZ has no limitation */ openvzError(VIR_ERR_INVALID_ARG, _("unknown type '%s'"), type); return -1; }
static int hypervConnectIsSecure(virConnectPtr conn) { hypervPrivate *priv = conn->privateData; if (STRCASEEQ(priv->parsedUri->transport, "https")) { return 1; } else { return 0; } }
/* Similar to Tcl_GetBoolean. */ int guestfs_int_is_true (const char *str) { if (STREQ (str, "1") || STRCASEEQ (str, "true") || STRCASEEQ (str, "t") || STRCASEEQ (str, "yes") || STRCASEEQ (str, "y") || STRCASEEQ (str, "on")) return 1; if (STREQ (str, "0") || STRCASEEQ (str, "false") || STRCASEEQ (str, "f") || STRCASEEQ (str, "no") || STRCASEEQ (str, "n") || STRCASEEQ (str, "off")) return 0; return -1; }
static void s_get_tag_and_class_and_id(Doc *doc, Node *node, char **tag_name, char **class_name, char **id) { Attr *attr; *tag_name = node->name; for (attr = qs_get_attr(doc, node); attr; attr = qs_get_next_attr(doc,attr)) { char *name = qs_get_attr_name(doc,attr); char *value = qs_get_attr_value(doc,attr); if (STRCASEEQ('c','C', "class", name)) { if (*value != 0) { *class_name = value; } } else if (STRCASEEQ('i','I', "id", name)) { if (*value != 0) { *id = value; } } if (*id && *class_name) break; } }
/** * <devices> */ static void s_set_devices_data(Doc *doc, apr_pool_t *p, mod_chxj_config *conf, Node *node) { Node *child; for (child = qs_get_child_node(doc,node); child ; child = qs_get_next_node(doc,child)) { char *name = qs_get_node_name(doc,child); if (STRCASEEQ('d','D',"devices",name)) { s_set_user_agent_data(doc, p, conf, child); } } }
int hypervParseUri(hypervParsedUri **parsedUri, virURIPtr uri) { int result = -1; size_t i; if (parsedUri == NULL || *parsedUri != NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); return -1; } if (VIR_ALLOC(*parsedUri) < 0) return -1; for (i = 0; i < uri->paramsCount; i++) { virURIParamPtr queryParam = &uri->params[i]; if (STRCASEEQ(queryParam->name, "transport")) { VIR_FREE((*parsedUri)->transport); if (VIR_STRDUP((*parsedUri)->transport, queryParam->value) < 0) goto cleanup; if (STRNEQ((*parsedUri)->transport, "http") && STRNEQ((*parsedUri)->transport, "https")) { virReportError(VIR_ERR_INVALID_ARG, _("Query parameter 'transport' has unexpected value " "'%s' (should be http|https)"), (*parsedUri)->transport); goto cleanup; } } else { VIR_WARN("Ignoring unexpected query parameter '%s'", queryParam->name); } } if (!(*parsedUri)->transport && VIR_STRDUP((*parsedUri)->transport, "https") < 0) goto cleanup; result = 0; cleanup: if (result < 0) hypervFreeParsedUri(parsedUri); return result; }
char * u8_strconv_to_encoding (const uint8_t *string, const char *tocode, enum iconv_ilseq_handler handler) { char *result; size_t length; if (STRCASEEQ (tocode, "UTF-8", 'U','T','F','-','8',0,0,0,0)) { /* Conversion from UTF-8 to UTF-8. No need to go through iconv(). */ length = u8_strlen (string) + 1; #if CONFIG_UNICODE_SAFETY if (u8_check (string, length)) { errno = EILSEQ; return NULL; } #endif result = (char *) malloc (length); if (result == NULL) { errno = ENOMEM; return NULL; } memcpy (result, (const char *) string, length); return result; } else { result = NULL; length = 0; if (mem_iconveha ((const char *) string, u8_strlen (string) + 1, "UTF-8", tocode, handler == iconveh_question_mark, handler, NULL, &result, &length) < 0) return NULL; /* Verify the result has exactly one NUL byte, at the end. */ if (!(length > 0 && result[length-1] == '\0' && strlen (result) == length-1)) { free (result); errno = EILSEQ; return NULL; } return result; } }
char * do_debug (const char *subcmd, char *const *argv) { size_t argc, i; for (i = argc = 0; argv[i] != NULL; ++i) argc++; for (i = 0; cmds[i].cmd != NULL; ++i) { if (STRCASEEQ (subcmd, cmds[i].cmd)) return cmds[i].f (subcmd, argc, argv); } reply_with_error ("use 'debug help 0' to list the supported commands"); return NULL; }
virDomainCapsCPUModelPtr virDomainCapsCPUModelsGet(virDomainCapsCPUModelsPtr cpuModels, const char *name) { size_t i; if (!cpuModels) return NULL; for (i = 0; i < cpuModels->nmodels; i++) { if (STRCASEEQ(cpuModels->models[i].name, name)) return cpuModels->models + i; } return NULL; }
int xenapiUtil_ParseQuery(virConnectPtr conn, xmlURIPtr uri, int *noVerify) { int result = 0; int i; struct qparam_set *queryParamSet = NULL; struct qparam *queryParam = NULL; #ifdef HAVE_XMLURI_QUERY_RAW queryParamSet = qparam_query_parse(uri->query_raw); #else queryParamSet = qparam_query_parse(uri->query); #endif if (queryParamSet == NULL) { goto failure; } for (i = 0; i < queryParamSet->n; i++) { queryParam = &queryParamSet->p[i]; if (STRCASEEQ(queryParam->name, "no_verify")) { if (noVerify == NULL) { continue; } if (virStrToLong_i(queryParam->value, NULL, 10, noVerify) < 0 || (*noVerify != 0 && *noVerify != 1)) { xenapiSessionErrorHandler(conn, VIR_ERR_INVALID_ARG, _("Query parameter 'no_verify' has unexpected value (should be 0 or 1)")); goto failure; } } } cleanup: if (queryParamSet != NULL) { free_qparam_set(queryParamSet); } return result; failure: result = -1; goto cleanup; }
/* Matches Windows registry HKLM\SYSYTEM\MountedDevices\DosDevices blob to * to libguestfs GPT partition device. For GPT disks, the blob is made of * "DMIO:ID:" prefix followed by the GPT partition GUID. */ static char * map_registry_disk_blob_gpt (guestfs_h *g, const void *blob) { CLEANUP_FREE_STRING_LIST char **parts = NULL; CLEANUP_FREE char *blob_guid = extract_guid_from_registry_blob (g, blob); size_t i; parts = guestfs_list_partitions (g); if (parts == NULL) return NULL; for (i = 0; parts[i] != NULL; ++i) { CLEANUP_FREE char *fs_guid = NULL; int partnum; CLEANUP_FREE char *device = NULL; CLEANUP_FREE char *type = NULL; partnum = guestfs_part_to_partnum (g, parts[i]); if (partnum == -1) continue; device = guestfs_part_to_dev (g, parts[i]); if (device == NULL) continue; type = guestfs_part_get_parttype (g, device); if (type == NULL) continue; if (STRCASENEQ (type, "gpt")) continue; /* get the GPT parition GUID from the partition block device */ fs_guid = guestfs_part_get_gpt_guid (g, device, partnum); if (fs_guid == NULL) continue; /* if both GUIDs match, we have found the mapping for our device */ if (STRCASEEQ (fs_guid, blob_guid)) return safe_strdup (g, parts[i]); } return NULL; }
/** * virConfGetValue: * @conf: a configuration file handle * @entry: the name of the entry * * Lookup the value associated to this entry in the configuration file * * Returns a pointer to the value or NULL if the lookup failed, the data * associated will be freed when virConfFree() is called */ virConfValuePtr virConfGetValue(virConfPtr conf, const char *setting) { virConfEntryPtr cur; if (conf == NULL) return NULL; cur = conf->entries; while (cur != NULL) { if ((cur->name != NULL) && ((conf->flags & VIR_CONF_FLAG_VMX_FORMAT && STRCASEEQ(cur->name, setting)) || STREQ(cur->name, setting))) return cur->value; cur = cur->next; } return NULL; }
int virInterfaceFindByMACString(const virInterfaceObjListPtr interfaces, const char *mac, virInterfaceObjPtr *matches, int maxmatches) { unsigned int i, matchct = 0; for (i = 0 ; i < interfaces->count ; i++) { virInterfaceObjLock(interfaces->objs[i]); if (STRCASEEQ(interfaces->objs[i]->def->mac, mac)) { matchct++; if (matchct <= maxmatches) { matches[matchct - 1] = interfaces->objs[i]; /* keep the lock if we're returning object to caller */ /* it is the caller's responsibility to unlock *all* matches */ continue; } } virInterfaceObjUnlock(interfaces->objs[i]); } return matchct; }
static const char * program_of_csum (const char *csumtype) { if (STRCASEEQ (csumtype, "crc")) return str_cksum; else if (STRCASEEQ (csumtype, "md5")) return str_md5sum; else if (STRCASEEQ (csumtype, "sha1")) return str_sha1sum; else if (STRCASEEQ (csumtype, "sha224")) return str_sha224sum; else if (STRCASEEQ (csumtype, "sha256")) return str_sha256sum; else if (STRCASEEQ (csumtype, "sha384")) return str_sha384sum; else if (STRCASEEQ (csumtype, "sha512")) return str_sha512sum; else { reply_with_error ("unknown checksum type, expecting crc|md5|sha1|sha224|sha256|sha384|sha512"); return NULL; } }
static int check_windows_system_registry (guestfs_h *g, struct inspect_fs *fs) { int r; size_t len = strlen (fs->windows_systemroot) + 64; char system[len]; snprintf (system, len, "%s/system32/config/system", fs->windows_systemroot); CLEANUP_FREE char *system_path = guestfs_case_sensitive_path (g, system); if (!system_path) return -1; r = guestfs_is_file (g, system_path); if (r == -1) return -1; /* If the system hive doesn't exist, just accept that we cannot * find hostname etc. */ if (r == 0) return 0; int ret = -1; int64_t root, node, value; CLEANUP_FREE_HIVEX_VALUE_LIST struct guestfs_hivex_value_list *values = NULL; CLEANUP_FREE_HIVEX_VALUE_LIST struct guestfs_hivex_value_list *values2 = NULL; int32_t dword; size_t i, count; CLEANUP_FREE void *buf = NULL; size_t buflen; const char *hivepath[] = { NULL /* current control set */, "Services", "Tcpip", "Parameters" }; if (guestfs_hivex_open (g, system_path, GUESTFS_HIVEX_OPEN_VERBOSE, g->verbose, -1) == -1) goto out; root = guestfs_hivex_root (g); if (root == 0) goto out; /* Get the CurrentControlSet. */ node = guestfs_hivex_node_get_child (g, root, "Select"); if (node == -1) goto out; if (node == 0) { error (g, "hivex: could not locate HKLM\\SYSTEM\\Select"); goto out; } value = guestfs_hivex_node_get_value (g, node, "Current"); if (value == -1) goto out; if (value == 0) { error (g, "hivex: HKLM\\System\\Select Default entry not found"); goto out; } /* XXX Should check the type. */ buf = guestfs_hivex_value_value (g, value, &buflen); if (buflen != 4) { error (g, "hivex: HKLM\\System\\Select\\Current expected to be DWORD"); goto out; } dword = le32toh (*(int32_t *)buf); fs->windows_current_control_set = safe_asprintf (g, "ControlSet%03d", dword); /* Get the drive mappings. * This page explains the contents of HKLM\System\MountedDevices: * http://www.goodells.net/multiboot/partsigs.shtml */ node = guestfs_hivex_node_get_child (g, root, "MountedDevices"); if (node == -1) goto out; if (node == 0) /* Not found: skip getting drive letter mappings (RHBZ#803664). */ goto skip_drive_letter_mappings; values = guestfs_hivex_node_values (g, node); /* Count how many DOS drive letter mappings there are. This doesn't * ignore removable devices, so it overestimates, but that doesn't * matter because it just means we'll allocate a few bytes extra. */ for (i = count = 0; i < values->len; ++i) { CLEANUP_FREE char *key = guestfs_hivex_value_key (g, values->val[i].hivex_value_h); if (key == NULL) goto out; if (STRCASEEQLEN (key, "\\DosDevices\\", 12) && c_isalpha (key[12]) && key[13] == ':') count++; } fs->drive_mappings = safe_calloc (g, 2*count + 1, sizeof (char *)); for (i = count = 0; i < values->len; ++i) { int64_t v = values->val[i].hivex_value_h; CLEANUP_FREE char *key = guestfs_hivex_value_key (g, v); if (key == NULL) goto out; if (STRCASEEQLEN (key, "\\DosDevices\\", 12) && c_isalpha (key[12]) && key[13] == ':') { /* Get the binary value. Is it a fixed disk? */ CLEANUP_FREE char *blob = NULL; char *device; size_t len; int64_t type; type = guestfs_hivex_value_type (g, v); blob = guestfs_hivex_value_value (g, v, &len); if (blob != NULL && type == 3 && len == 12) { /* Try to map the blob to a known disk and partition. */ device = map_registry_disk_blob (g, blob); if (device != NULL) { fs->drive_mappings[count++] = safe_strndup (g, &key[12], 1); fs->drive_mappings[count++] = device; } } } } skip_drive_letter_mappings:; /* Get the hostname. */ hivepath[0] = fs->windows_current_control_set; for (node = root, i = 0; node > 0 && i < sizeof hivepath / sizeof hivepath[0]; ++i) { node = guestfs_hivex_node_get_child (g, node, hivepath[i]); } if (node == -1) goto out; if (node == 0) { perrorf (g, "hivex: cannot locate HKLM\\SYSTEM\\%s\\Services\\Tcpip\\Parameters", fs->windows_current_control_set); goto out; } values2 = guestfs_hivex_node_values (g, node); if (values2 == NULL) goto out; for (i = 0; i < values2->len; ++i) { int64_t v = values2->val[i].hivex_value_h; CLEANUP_FREE char *key = guestfs_hivex_value_key (g, v); if (key == NULL) goto out; if (STRCASEEQ (key, "Hostname")) { fs->hostname = guestfs_hivex_value_utf8 (g, v); if (!fs->hostname) goto out; } /* many other interesting fields here ... */ } ret = 0; out: guestfs_hivex_close (g); return ret; }
/* At the moment, pull just the ProductName and version numbers from * the registry. In future there is a case for making many more * registry fields available to callers. */ static int check_windows_software_registry (guestfs_h *g, struct inspect_fs *fs) { int ret = -1; int r; size_t len = strlen (fs->windows_systemroot) + 64; char software[len]; snprintf (software, len, "%s/system32/config/software", fs->windows_systemroot); CLEANUP_FREE char *software_path = guestfs_case_sensitive_path (g, software); if (!software_path) return -1; r = guestfs_is_file (g, software_path); if (r == -1) return -1; /* If the software hive doesn't exist, just accept that we cannot * find product_name etc. */ if (r == 0) return 0; int64_t node; const char *hivepath[] = { "Microsoft", "Windows NT", "CurrentVersion" }; size_t i; CLEANUP_FREE_HIVEX_VALUE_LIST struct guestfs_hivex_value_list *values = NULL; if (guestfs_hivex_open (g, software_path, GUESTFS_HIVEX_OPEN_VERBOSE, g->verbose, -1) == -1) return -1; node = guestfs_hivex_root (g); for (i = 0; node > 0 && i < sizeof hivepath / sizeof hivepath[0]; ++i) node = guestfs_hivex_node_get_child (g, node, hivepath[i]); if (node == -1) goto out; if (node == 0) { perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"); goto out; } values = guestfs_hivex_node_values (g, node); for (i = 0; i < values->len; ++i) { int64_t value = values->val[i].hivex_value_h; CLEANUP_FREE char *key = guestfs_hivex_value_key (g, value); if (key == NULL) goto out; if (STRCASEEQ (key, "ProductName")) { fs->product_name = guestfs_hivex_value_utf8 (g, value); if (!fs->product_name) goto out; } else if (STRCASEEQ (key, "CurrentVersion")) { CLEANUP_FREE char *version = guestfs_hivex_value_utf8 (g, value); if (!version) goto out; char *major, *minor; if (match2 (g, version, re_windows_version, &major, &minor)) { fs->major_version = guestfs___parse_unsigned_int (g, major); free (major); if (fs->major_version == -1) { free (minor); goto out; } fs->minor_version = guestfs___parse_unsigned_int (g, minor); free (minor); if (fs->minor_version == -1) goto out; } } else if (STRCASEEQ (key, "InstallationType")) { fs->product_variant = guestfs_hivex_value_utf8 (g, value); if (!fs->product_variant) goto out; } } ret = 0; out: guestfs_hivex_close (g); return ret; }
char * u8_conv_to_encoding (const char *tocode, enum iconv_ilseq_handler handler, const uint8_t *src, size_t srclen, size_t *offsets, char *resultbuf, size_t *lengthp) { if (STRCASEEQ (tocode, "UTF-8", 'U','T','F','-','8',0,0,0,0)) { char *result; /* Conversion from UTF-8 to UTF-8. No need to go through iconv(). */ #if CONFIG_UNICODE_SAFETY if (u8_check (src, srclen)) { errno = EILSEQ; return NULL; } #endif /* Memory allocation. */ if (resultbuf != NULL && *lengthp >= srclen) result = resultbuf; else { result = (char *) malloc (srclen > 0 ? srclen : 1); if (result == NULL) { errno = ENOMEM; return NULL; } } memcpy (result, (const char *) src, srclen); *lengthp = srclen; return result; } else { char *result = resultbuf; size_t length = *lengthp; if (mem_iconveha ((const char *) src, srclen, "UTF-8", tocode, handler == iconveh_question_mark, handler, offsets, &result, &length) < 0) return NULL; if (result == NULL) /* when (resultbuf == NULL && length == 0) */ { result = (char *) malloc (1); if (result == NULL) { errno = ENOMEM; return NULL; } } *lengthp = length; return result; } }
/* At the moment, pull just the ProductName and version numbers from * the registry. In future there is a case for making many more * registry fields available to callers. */ static int check_windows_software_registry (guestfs_h *g, struct inspect_fs *fs) { int ret = -1; int r; CLEANUP_FREE char *software = safe_asprintf (g, "%s/system32/config/software", fs->windows_systemroot); CLEANUP_FREE char *software_path = guestfs_case_sensitive_path (g, software); if (!software_path) return -1; r = guestfs_is_file (g, software_path); if (r == -1) return -1; /* If the software hive doesn't exist, just accept that we cannot * find product_name etc. */ if (r == 0) return 0; int64_t node; const char *hivepath[] = { "Microsoft", "Windows NT", "CurrentVersion" }; size_t i; CLEANUP_FREE_HIVEX_VALUE_LIST struct guestfs_hivex_value_list *values = NULL; bool ignore_currentversion = false; if (guestfs_hivex_open (g, software_path, GUESTFS_HIVEX_OPEN_VERBOSE, g->verbose, -1) == -1) return -1; node = guestfs_hivex_root (g); for (i = 0; node > 0 && i < sizeof hivepath / sizeof hivepath[0]; ++i) node = guestfs_hivex_node_get_child (g, node, hivepath[i]); if (node == -1) goto out; if (node == 0) { perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"); goto out; } values = guestfs_hivex_node_values (g, node); for (i = 0; i < values->len; ++i) { int64_t value = values->val[i].hivex_value_h; CLEANUP_FREE char *key = guestfs_hivex_value_key (g, value); if (key == NULL) goto out; if (STRCASEEQ (key, "ProductName")) { fs->product_name = guestfs_hivex_value_utf8 (g, value); if (!fs->product_name) goto out; } else if (STRCASEEQ (key, "CurrentMajorVersionNumber")) { size_t vsize; int64_t vtype = guestfs_hivex_value_type (g, value); CLEANUP_FREE char *vbuf = guestfs_hivex_value_value (g, value, &vsize); if (vbuf == NULL) goto out; if (vtype != 4 || vsize != 4) { error (g, "hivex: expected CurrentVersion\\%s to be a DWORD field", "CurrentMajorVersionNumber"); goto out; } fs->version.v_major = le32toh (*(int32_t *)vbuf); /* Ignore CurrentVersion if we see it after this key. */ ignore_currentversion = true; } else if (STRCASEEQ (key, "CurrentMinorVersionNumber")) { size_t vsize; int64_t vtype = guestfs_hivex_value_type (g, value); CLEANUP_FREE char *vbuf = guestfs_hivex_value_value (g, value, &vsize); if (vbuf == NULL) goto out; if (vtype != 4 || vsize != 4) { error (g, "hivex: expected CurrentVersion\\%s to be a DWORD field", "CurrentMinorVersionNumber"); goto out; } fs->version.v_minor = le32toh (*(int32_t *)vbuf); /* Ignore CurrentVersion if we see it after this key. */ ignore_currentversion = true; } else if (!ignore_currentversion && STRCASEEQ (key, "CurrentVersion")) { CLEANUP_FREE char *version = guestfs_hivex_value_utf8 (g, value); if (!version) goto out; if (guestfs_int_version_from_x_y_re (g, &fs->version, version, re_windows_version) == -1) goto out; } else if (STRCASEEQ (key, "InstallationType")) { fs->product_variant = guestfs_hivex_value_utf8 (g, value); if (!fs->product_variant) goto out; } } ret = 0; out: guestfs_hivex_close (g); return ret; }
uint8_t * u8_conv_from_encoding (const char *fromcode, enum iconv_ilseq_handler handler, const char *src, size_t srclen, size_t *offsets, uint8_t *resultbuf, size_t *lengthp) { if (STRCASEEQ (fromcode, "UTF-8", 'U','T','F','-','8',0,0,0,0)) { /* Conversion from UTF-8 to UTF-8. No need to go through iconv(). */ uint8_t *result; if (u8_check ((const uint8_t *) src, srclen)) { errno = EILSEQ; return NULL; } if (offsets != NULL) { size_t i; for (i = 0; i < srclen; ) { int count = u8_mblen ((const uint8_t *) src + i, srclen - i); /* We can rely on count > 0 because of the previous u8_check. */ if (count <= 0) abort (); offsets[i] = i; i++; while (--count > 0) offsets[i++] = (size_t)(-1); } } /* Memory allocation. */ if (resultbuf != NULL && *lengthp >= srclen) result = resultbuf; else { result = (uint8_t *) malloc (srclen > 0 ? srclen : 1); if (result == NULL) { errno = ENOMEM; return NULL; } } memcpy ((char *) result, src, srclen); *lengthp = srclen; return result; } else { char *result = (char *) resultbuf; size_t length = *lengthp; if (mem_iconveha (src, srclen, fromcode, "UTF-8", true, handler, offsets, &result, &length) < 0) return NULL; if (result == NULL) /* when (resultbuf == NULL && length == 0) */ { result = (char *) malloc (1); if (result == NULL) { errno = ENOMEM; return NULL; } } *lengthp = length; return (uint8_t *) result; } }
int run_edit (const char *cmd, size_t argc, char *argv[]) { CLEANUP_FREE char *tmpdir = guestfs_get_tmpdir (g); CLEANUP_UNLINK_FREE char *filename = NULL; char buf[256]; const char *editor; CLEANUP_FREE char *remotefilename = NULL, *newname = NULL; struct stat oldstat, newstat; int r, fd; if (argc != 1) { fprintf (stderr, _("use '%s filename' to edit a file\n"), cmd); return -1; } /* Choose an editor. */ if (STRCASEEQ (cmd, "vi")) editor = "vi"; else if (STRCASEEQ (cmd, "emacs")) editor = "emacs -nw"; else { editor = getenv ("EDITOR"); if (editor == NULL) editor = "vi"; /* could be cruel here and choose ed(1) */ } /* Handle 'win:...' prefix. */ remotefilename = win_prefix (argv[0]); if (remotefilename == NULL) return -1; /* Download the file and write it to a temporary. */ if (asprintf (&filename, "%s/guestfishXXXXXX", tmpdir) == -1) { perror ("asprintf"); return -1; } fd = mkstemp (filename); if (fd == -1) { perror ("mkstemp"); return -1; } snprintf (buf, sizeof buf, "/dev/fd/%d", fd); if (guestfs_download (g, remotefilename, buf) == -1) { close (fd); return -1; } if (close (fd) == -1) { perror (filename); return -1; } /* Get the old stat. */ if (stat (filename, &oldstat) == -1) { perror (filename); return -1; } /* Edit it. */ /* XXX Safe? */ snprintf (buf, sizeof buf, "%s %s", editor, filename); r = system (buf); if (r != 0) { perror (buf); return -1; } /* Get the new stat. */ if (stat (filename, &newstat) == -1) { perror (filename); return -1; } /* Changed? */ if (oldstat.st_ctime == newstat.st_ctime && oldstat.st_size == newstat.st_size) return 0; /* Upload to a new file in the same directory, so if it fails we * don't end up with a partially written file. Give the new file * a completely random name so we have only a tiny chance of * overwriting some existing file. */ newname = generate_random_name (remotefilename); if (!newname) return -1; /* Write new content. */ if (guestfs_upload (g, filename, newname) == -1) return -1; /* Set the permissions, UID, GID and SELinux context of the new * file to match the old file (RHBZ#788641). */ if (guestfs_copy_attributes (g, remotefilename, newname, GUESTFS_COPY_ATTRIBUTES_ALL, 1, -1) == -1) return -1; if (guestfs_mv (g, newname, remotefilename) == -1) return -1; return 0; }
static char * esxStoragePoolGetXMLDesc(virStoragePoolPtr pool, unsigned int flags) { esxPrivate *priv = pool->conn->privateData; esxVI_String *propertyNameList = NULL; esxVI_ObjectContent *datastore = NULL; esxVI_DatastoreHostMount *hostMount = NULL; esxVI_DynamicProperty *dynamicProperty = NULL; esxVI_Boolean accessible = esxVI_Boolean_Undefined; virStoragePoolDef def; esxVI_DatastoreInfo *info = NULL; esxVI_NasDatastoreInfo *nasInfo = NULL; char *xml = NULL; virCheckFlags(0, NULL); memset(&def, 0, sizeof(def)); if (esxVI_String_AppendValueListToList(&propertyNameList, "summary.accessible\0" "summary.capacity\0" "summary.freeSpace\0" "info\0") < 0 || esxVI_LookupDatastoreByName(priv->primary, pool->name, propertyNameList, &datastore, esxVI_Occurrence_RequiredItem) < 0 || esxVI_GetBoolean(datastore, "summary.accessible", &accessible, esxVI_Occurrence_RequiredItem) < 0 || esxVI_LookupDatastoreHostMount(priv->primary, datastore->obj, &hostMount, esxVI_Occurrence_RequiredItem) < 0) { goto cleanup; } def.name = pool->name; memcpy(def.uuid, pool->uuid, VIR_UUID_BUFLEN); def.target.path = hostMount->mountInfo->path; if (accessible == esxVI_Boolean_True) { for (dynamicProperty = datastore->propSet; dynamicProperty; dynamicProperty = dynamicProperty->_next) { if (STREQ(dynamicProperty->name, "summary.capacity")) { if (esxVI_AnyType_ExpectType(dynamicProperty->val, esxVI_Type_Long) < 0) { goto cleanup; } def.capacity = dynamicProperty->val->int64; } else if (STREQ(dynamicProperty->name, "summary.freeSpace")) { if (esxVI_AnyType_ExpectType(dynamicProperty->val, esxVI_Type_Long) < 0) { goto cleanup; } def.available = dynamicProperty->val->int64; } } def.allocation = def.capacity - def.available; } for (dynamicProperty = datastore->propSet; dynamicProperty; dynamicProperty = dynamicProperty->_next) { if (STREQ(dynamicProperty->name, "info")) { if (esxVI_DatastoreInfo_CastFromAnyType(dynamicProperty->val, &info) < 0) { goto cleanup; } break; } } /* See vSphere API documentation about HostDatastoreSystem for details */ if (esxVI_LocalDatastoreInfo_DynamicCast(info)) { def.type = VIR_STORAGE_POOL_DIR; } else if ((nasInfo = esxVI_NasDatastoreInfo_DynamicCast(info))) { if (VIR_ALLOC_N(def.source.hosts, 1) < 0) goto cleanup; def.type = VIR_STORAGE_POOL_NETFS; def.source.nhost = 1; def.source.hosts[0].name = nasInfo->nas->remoteHost; def.source.dir = nasInfo->nas->remotePath; if (STRCASEEQ(nasInfo->nas->type, "NFS")) { def.source.format = VIR_STORAGE_POOL_NETFS_NFS; } else if (STRCASEEQ(nasInfo->nas->type, "CIFS")) { def.source.format = VIR_STORAGE_POOL_NETFS_CIFS; } else { virReportError(VIR_ERR_INTERNAL_ERROR, _("Datastore has unexpected type '%s'"), nasInfo->nas->type); goto cleanup; } } else if (esxVI_VmfsDatastoreInfo_DynamicCast(info)) { def.type = VIR_STORAGE_POOL_FS; /* * FIXME: I'm not sure how to represent the source and target of a * VMFS based datastore in libvirt terms */ } else { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("DatastoreInfo has unexpected type")); goto cleanup; } xml = virStoragePoolDefFormat(&def); cleanup: esxVI_String_Free(&propertyNameList); esxVI_ObjectContent_Free(&datastore); esxVI_DatastoreHostMount_Free(&hostMount); esxVI_DatastoreInfo_Free(&info); return xml; }
int virNetDevVlanParse(xmlNodePtr node, xmlXPathContextPtr ctxt, virNetDevVlanPtr def) { int ret = -1; xmlNodePtr save = ctxt->node; char *trunk = NULL; char *nativeMode = NULL; xmlNodePtr *tagNodes = NULL; int nTags; size_t i; ctxt->node = node; nTags = virXPathNodeSet("./tag", ctxt, &tagNodes); if (nTags < 0) goto cleanup; if (nTags == 0) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing tag id - each <vlan> must have " "at least one <tag id='n'/> subelement")); goto cleanup; } if (VIR_ALLOC_N(def->tag, nTags) < 0) goto cleanup; def->nativeMode = 0; def->nativeTag = 0; for (i = 0; i < nTags; i++) { unsigned long id; ctxt->node = tagNodes[i]; if (virXPathULong("string(./@id)", ctxt, &id) < 0) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing or invalid vlan tag id attribute")); goto cleanup; } if (id > 4095) { virReportError(VIR_ERR_XML_ERROR, _("vlan tag id %lu too large (maximum 4095)"), id); goto cleanup; } if ((nativeMode = virXPathString("string(./@nativeMode)", ctxt))) { if (def->nativeMode != 0) { virReportError(VIR_ERR_XML_ERROR, "%s", _("duplicate native vlan setting")); goto cleanup; } if ((def->nativeMode = virNativeVlanModeTypeFromString(nativeMode)) <= 0) { virReportError(VIR_ERR_XML_ERROR, _("Invalid \"nativeMode='%s'\" " "in vlan <tag> element"), nativeMode); goto cleanup; } VIR_FREE(nativeMode); def->nativeTag = id; } def->tag[i] = id; } def->nTags = nTags; /* now that we know how many tags there are, look for an explicit * trunk setting. */ if (nTags > 1) def->trunk = true; ctxt->node = node; if ((trunk = virXPathString("string(./@trunk)", ctxt)) != NULL) { def->trunk = STRCASEEQ(trunk, "yes"); if (!def->trunk) { if (nTags > 1) { virReportError(VIR_ERR_XML_ERROR, _("invalid \"trunk='%s'\" in <vlan> - trunk='yes' " "is required for more than one vlan tag"), trunk); goto cleanup; } if (def->nativeMode != 0) { virReportError(VIR_ERR_XML_ERROR, "%s", _("invalid configuration in <vlan> - \"trunk='no'\" is " "not allowed with a native vlan id")); goto cleanup; } /* allow (but discard) "trunk='no' if there is a single tag */ if (STRCASENEQ(trunk, "no")) { virReportError(VIR_ERR_XML_ERROR, _("invalid \"trunk='%s'\" in <vlan> " "- must be yes or no"), trunk); goto cleanup; } } } ret = 0; cleanup: ctxt->node = save; VIR_FREE(tagNodes); VIR_FREE(trunk); VIR_FREE(nativeMode); if (ret < 0) virNetDevVlanClear(def); return ret; }
static virDrvOpenStatus hypervConnectOpen(virConnectPtr conn, virConnectAuthPtr auth, unsigned int flags) { virDrvOpenStatus result = VIR_DRV_OPEN_ERROR; char *plus; hypervPrivate *priv = NULL; char *username = NULL; char *password = NULL; virBuffer query = VIR_BUFFER_INITIALIZER; Msvm_ComputerSystem *computerSystem = NULL; virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR); /* Decline if the URI is NULL or the scheme is NULL */ if (conn->uri == NULL || conn->uri->scheme == NULL) { return VIR_DRV_OPEN_DECLINED; } /* Decline if the scheme is not hyperv */ plus = strchr(conn->uri->scheme, '+'); if (plus == NULL) { if (STRCASENEQ(conn->uri->scheme, "hyperv")) { return VIR_DRV_OPEN_DECLINED; } } else { if (plus - conn->uri->scheme != 6 || STRCASENEQLEN(conn->uri->scheme, "hyperv", 6)) { return VIR_DRV_OPEN_DECLINED; } virReportError(VIR_ERR_INVALID_ARG, _("Transport '%s' in URI scheme is not supported, try again " "without the transport part"), plus + 1); return VIR_DRV_OPEN_ERROR; } /* Require server part */ if (conn->uri->server == NULL) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("URI is missing the server part")); return VIR_DRV_OPEN_ERROR; } /* Require auth */ if (auth == NULL || auth->cb == NULL) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("Missing or invalid auth pointer")); return VIR_DRV_OPEN_ERROR; } /* Allocate per-connection private data */ if (VIR_ALLOC(priv) < 0) goto cleanup; if (hypervParseUri(&priv->parsedUri, conn->uri) < 0) { goto cleanup; } /* Set the port dependent on the transport protocol if no port is * specified. This allows us to rely on the port parameter being * correctly set when building URIs later on, without the need to * distinguish between the situations port == 0 and port != 0 */ if (conn->uri->port == 0) { if (STRCASEEQ(priv->parsedUri->transport, "https")) { conn->uri->port = 5986; } else { conn->uri->port = 5985; } } /* Request credentials */ if (conn->uri->user != NULL) { if (VIR_STRDUP(username, conn->uri->user) < 0) goto cleanup; } else { username = virAuthGetUsername(conn, auth, "hyperv", "administrator", conn->uri->server); if (username == NULL) { virReportError(VIR_ERR_AUTH_FAILED, "%s", _("Username request failed")); goto cleanup; } } password = virAuthGetPassword(conn, auth, "hyperv", username, conn->uri->server); if (password == NULL) { virReportError(VIR_ERR_AUTH_FAILED, "%s", _("Password request failed")); goto cleanup; } /* Initialize the openwsman connection */ priv->client = wsmc_create(conn->uri->server, conn->uri->port, "/wsman", priv->parsedUri->transport, username, password); if (priv->client == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Could not create openwsman client")); goto cleanup; } if (wsmc_transport_init(priv->client, NULL) != 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Could not initialize openwsman transport")); goto cleanup; } /* FIXME: Currently only basic authentication is supported */ wsman_transport_set_auth_method(priv->client, "basic"); /* Check if the connection can be established and if the server has the * Hyper-V role installed. If the call to hypervGetMsvmComputerSystemList * succeeds than the connection has been established. If the returned list * is empty than the server isn't a Hyper-V server. */ virBufferAddLit(&query, MSVM_COMPUTERSYSTEM_WQL_SELECT); virBufferAddLit(&query, "where "); virBufferAddLit(&query, MSVM_COMPUTERSYSTEM_WQL_PHYSICAL); if (hypervGetMsvmComputerSystemList(priv, &query, &computerSystem) < 0) { goto cleanup; } if (computerSystem == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, _("%s is not a Hyper-V server"), conn->uri->server); goto cleanup; } conn->privateData = priv; priv = NULL; result = VIR_DRV_OPEN_SUCCESS; cleanup: hypervFreePrivate(&priv); VIR_FREE(username); VIR_FREE(password); hypervFreeObject(priv, (hypervObject *)computerSystem); return result; }
static virDrvOpenStatus hypervConnectOpen(virConnectPtr conn, virConnectAuthPtr auth, virConfPtr conf ATTRIBUTE_UNUSED, unsigned int flags) { virDrvOpenStatus result = VIR_DRV_OPEN_ERROR; char *plus; hypervPrivate *priv = NULL; char *username = NULL; char *password = NULL; virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR); /* Decline if the URI is NULL or the scheme is NULL */ if (conn->uri == NULL || conn->uri->scheme == NULL) return VIR_DRV_OPEN_DECLINED; /* Decline if the scheme is not hyperv */ plus = strchr(conn->uri->scheme, '+'); if (plus == NULL) { if (STRCASENEQ(conn->uri->scheme, "hyperv")) return VIR_DRV_OPEN_DECLINED; } else { if (plus - conn->uri->scheme != 6 || STRCASENEQLEN(conn->uri->scheme, "hyperv", 6)) { return VIR_DRV_OPEN_DECLINED; } virReportError(VIR_ERR_INVALID_ARG, _("Transport '%s' in URI scheme is not supported, try again " "without the transport part"), plus + 1); return VIR_DRV_OPEN_ERROR; } /* Require server part */ if (conn->uri->server == NULL) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("URI is missing the server part")); return VIR_DRV_OPEN_ERROR; } /* Require auth */ if (auth == NULL || auth->cb == NULL) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("Missing or invalid auth pointer")); return VIR_DRV_OPEN_ERROR; } /* Allocate per-connection private data */ if (VIR_ALLOC(priv) < 0) goto cleanup; if (hypervParseUri(&priv->parsedUri, conn->uri) < 0) goto cleanup; /* Set the port dependent on the transport protocol if no port is * specified. This allows us to rely on the port parameter being * correctly set when building URIs later on, without the need to * distinguish between the situations port == 0 and port != 0 */ if (conn->uri->port == 0) { if (STRCASEEQ(priv->parsedUri->transport, "https")) { conn->uri->port = 5986; } else { conn->uri->port = 5985; } } /* Request credentials */ if (conn->uri->user != NULL) { if (VIR_STRDUP(username, conn->uri->user) < 0) goto cleanup; } else { username = virAuthGetUsername(conn, auth, "hyperv", "administrator", conn->uri->server); if (username == NULL) { virReportError(VIR_ERR_AUTH_FAILED, "%s", _("Username request failed")); goto cleanup; } } password = virAuthGetPassword(conn, auth, "hyperv", username, conn->uri->server); if (password == NULL) { virReportError(VIR_ERR_AUTH_FAILED, "%s", _("Password request failed")); goto cleanup; } if (hypervInitConnection(conn, priv, username, password) < 0) goto cleanup; conn->privateData = priv; priv = NULL; result = VIR_DRV_OPEN_SUCCESS; cleanup: hypervFreePrivate(&priv); VIR_FREE(username); VIR_FREE(password); return result; }