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
|
/* libguestfs - mini library for parsing -a URI parameters
* Copyright (C) 2013 Red Hat 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.
*/
/**
* This file implements URI parsing for the I<-a> option, in many
* utilities including L<guestfish(1)>, L<virt-cat(1)>,
* L<virt-builder(1)>, L<virt-customize(1)>, etc.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <libintl.h>
#include <libxml/uri.h>
#include "c-ctype.h"
#include "getprogname.h"
#include "guestfs.h"
#include "guestfs-utils.h"
#include "uri.h"
static int is_uri (const char *arg);
static int parse (const char *arg, char **path_ret, char **protocol_ret, char ***server_ret, char **username_ret, char **password_ret);
static char *query_get (xmlURIPtr uri, const char *search_name);
static int make_server (xmlURIPtr uri, const char *socket, char ***ret);
int
parse_uri (const char *arg, struct uri *uri_ret)
{
char *path = NULL;
char *protocol = NULL;
char **server = NULL;
char *username = NULL;
char *password = NULL;
/* Does it look like a URI? */
if (is_uri (arg)) {
if (parse (arg, &path, &protocol, &server, &username, &password) == -1)
return -1;
}
else {
/* Ordinary file. */
path = strdup (arg);
if (!path) {
perror ("strdup");
return -1;
}
protocol = strdup ("file");
if (!protocol) {
perror ("strdup");
free (path);
return -1;
}
}
uri_ret->path = path;
uri_ret->protocol = protocol;
uri_ret->server = server;
uri_ret->username = username;
uri_ret->password = password;
return 0;
}
/* Does it "look like" a URI? A short lower-case ASCII string
* followed by "://" will do. Note that we properly parse the URI
* later on using libxml2.
*/
static int
is_uri (const char *arg)
{
const char *p;
p = strstr (arg, "://");
if (!p)
return 0;
if (p - arg > 8)
return 0;
for (p--; p >= arg; p--) {
if (!c_islower (*p))
return 0;
}
return 1;
}
static int
parse (const char *arg, char **path_ret, char **protocol_ret,
char ***server_ret, char **username_ret, char **password_ret)
{
CLEANUP_XMLFREEURI xmlURIPtr uri = NULL;
CLEANUP_FREE char *socket = NULL;
char *path;
uri = xmlParseURI (arg);
if (!uri) {
fprintf (stderr, _("%s: --add: could not parse URI ā%sā\n"),
getprogname (), arg);
return -1;
}
/* Note we don't do much checking of the parsed URI, since the
* underlying function 'guestfs_add_drive_opts' will check for us.
* So just the basics here.
*/
if (uri->scheme == NULL || STREQ (uri->scheme, "")) {
/* Probably can never happen. */
fprintf (stderr, _("%s: %s: scheme of URI is NULL or empty\n"),
getprogname (), arg);
return -1;
}
socket = query_get (uri, "socket");
if (uri->server && STRNEQ (uri->server, "") && socket) {
fprintf (stderr, _("%s: %s: cannot both a server name and a socket query parameter\n"),
getprogname (), arg);
return -1;
}
/* Is this needed? XXX
if (socket && socket[0] != '/') {
fprintf (stderr, _("%s: --add %s: socket query parameter must be an absolute path\n"),
getprogname (), arg);
return -1;
}
*/
*protocol_ret = strdup (uri->scheme);
if (*protocol_ret == NULL) {
perror ("strdup: protocol");
return -1;
}
if (make_server (uri, socket, server_ret) == -1) {
free (*protocol_ret);
return -1;
}
*password_ret = NULL;
*username_ret = NULL;
if (uri->user && STRNEQ (uri->user, "")) {
char *p = strchr (uri->user, ':');
if (p != NULL) {
if (STRNEQ (p+1, "")) {
*password_ret = strdup (p+1);
if (*password_ret == NULL) {
perror ("strdup: password");
free (*protocol_ret);
guestfs_int_free_string_list (*server_ret);
return -1;
}
}
*p = '\0';
}
*username_ret = strdup (uri->user);
if (*username_ret == NULL) {
perror ("strdup: username");
free (*password_ret);
free (*protocol_ret);
guestfs_int_free_string_list (*server_ret);
return -1;
}
}
/* We may have to adjust the path depending on the protocol. For
* example ceph/rbd URIs look like rbd:///pool/disk, but the
* exportname expected will be "pool/disk". Here, uri->path will be
* "/pool/disk" so we have to knock off the leading '/' character.
*/
path = uri->path;
if (path && path[0] == '/' &&
(STREQ (uri->scheme, "gluster") ||
STREQ (uri->scheme, "iscsi") ||
STREQ (uri->scheme, "nbd") ||
STREQ (uri->scheme, "rbd") ||
STREQ (uri->scheme, "sheepdog")))
path++;
*path_ret = strdup (path ? path : "");
if (*path_ret == NULL) {
perror ("strdup: path");
free (*protocol_ret);
guestfs_int_free_string_list (*server_ret);
free (*username_ret);
free (*password_ret);
return -1;
}
return 0;
}
/* Code inspired by libvirt src/util/viruri.c, written by danpb,
* released under a compatible license.
*/
static char *
query_get (xmlURIPtr uri, const char *search_name)
{
/* XXX libvirt uses deprecated uri->query field. Why? */
const char *query = uri->query_raw;
const char *end, *eq;
if (!query || STREQ (query, ""))
return NULL;
while (*query) {
CLEANUP_FREE char *name = NULL;
char *value = NULL;
/* Find the next separator, or end of the string. */
end = strchr (query, '&');
if (!end)
end = strchr(query, ';');
if (!end)
end = query + strlen (query);
/* Find the first '=' character between here and end. */
eq = strchr(query, '=');
if (eq && eq >= end) eq = NULL;
/* Empty section (eg. "&&"). */
if (end == query)
goto next;
/* If there is no '=' character, then we have just "name"
* and consistent with CGI.pm we assume value is "".
*/
else if (!eq) {
name = xmlURIUnescapeString (query, end - query, NULL);
if (!name) goto no_memory;
}
/* Or if we have "name=" here (works around annoying
* problem when calling xmlURIUnescapeString with len = 0).
*/
else if (eq+1 == end) {
name = xmlURIUnescapeString (query, eq - query, NULL);
if (!name) goto no_memory;
}
/* If the '=' character is at the beginning then we have
* "=value" and consistent with CGI.pm we _ignore_ this.
*/
else if (query == eq)
goto next;
/* Otherwise it's "name=value". */
else {
name = xmlURIUnescapeString (query, eq - query, NULL);
if (!name)
goto no_memory;
value = xmlURIUnescapeString (eq+1, end - (eq+1), NULL);
if (!value) {
goto no_memory;
}
}
/* Is it the name we're looking for? */
if (STREQ (name, search_name)) {
if (!value) {
value = strdup ("");
if (!value)
goto no_memory;
}
return value;
}
free (value);
next:
query = end;
if (*query)
query++; /* skip '&' separator */
}
/* search_name not found */
return NULL;
no_memory:
perror ("malloc");
return NULL;
}
/* Construct either a tcp: server list of a unix: server list or
* nothing at all from '-a' option URI.
*/
static int
make_server (xmlURIPtr uri, const char *socket, char ***ret)
{
char *server;
/* If the server part of the URI is specified, then this is a TCP
* connection.
*/
if (uri->server && STRNEQ (uri->server, "")) {
if (uri->port == 0) {
if (asprintf (&server, "tcp:%s", uri->server) == -1) {
perror ("asprintf");
return -1;
}
}
else {
if (asprintf (&server, "tcp:%s:%d", uri->server, uri->port) == -1) {
perror ("asprintf");
return -1;
}
}
}
/* Otherwise, ?socket query parameter means it's a Unix domain
* socket connection.
*/
else if (socket != NULL) {
if (asprintf (&server, "unix:%s", socket) == -1) {
perror ("asprintf");
return -1;
}
}
/* Otherwise, no server parameter is needed. */
else {
*ret = NULL;
return 0;
}
/* The .server parameter is in fact a list of strings, although
* only a singleton is passed by us.
*/
*ret = malloc (sizeof (char *) * 2);
if (*ret == NULL) {
perror ("malloc");
return -1;
}
(*ret)[0] = server;
(*ret)[1] = NULL;
return 0;
}
|