Update Red Hat Copyright Notices
[nbdkit.git] / plugins / curl / nbdkit-curl-plugin.pod
blob070f9a0f42a8a38b08bab9e9ff259a90c4153d61
1 =head1 NAME
3 nbdkit-curl-plugin - nbdkit curl plugin (HTTP, FTP and other protocols)
5 =head1 SYNOPSIS
7  nbdkit -r curl [url=]http://example.com/disk.img
9 =head1 DESCRIPTION
11 C<nbdkit-curl-plugin> is a plugin for L<nbdkit(1)> which turns content
12 served over HTTP, FTP, and more, into a Network Block Device.  It uses
13 a library called libcurl (also known as cURL) to read data from URLs.
14 The exact list of protocols that libcurl can handle depends on how it
15 was compiled, but most versions will handle HTTP, HTTPS, FTP, FTPS and
16 more (see: S<C<curl -V>>).
18 B<Note:> This plugin supports writes.  However for HTTP, you may not
19 want nbdkit to issue PUT requests to the remote server (which probably
20 doesn't understand them).  To force nbdkit to use a readonly
21 connection, pass the I<-r> flag.  Using the I<-r> flag also enables
22 NBD multi-conn, which usually performs much better (if the client
23 supports it).
25 Although this plugin can access SFTP (ie. SSH) servers, it is much
26 better to use L<nbdkit-ssh-plugin(1)>.  This plugin can be used to
27 access C<file:///> URLs, but you should use L<nbdkit-file-plugin(1)>
28 instead.
30 =head1 EXAMPLE
32  nbdkit -r curl http://example.com/disk.img
34 serves the remote disk image as NBD on TCP port 10809 (to control
35 ports and protocols used to serve NBD see L<nbdkit(1)>).
37 =head1 PARAMETERS
39 =over 4
41 =item B<cainfo=>FILENAME
43 (nbdkit E<ge> 1.18)
45 Configure CA bundle for libcurl. See L<CURLOPT_CAINFO(3)> for details.
47 Pass empty string in order to not use the default certificate store
48 that libcurl is compiled with.
50 =item B<capath=>PATH
52 (nbdkit E<ge> 1.18)
54 Set CA certificates directory location for libcurl. See
55 L<CURLOPT_CAPATH(3)> for more information.
57 =item B<connections=>N
59 (nbdkit E<ge> 1.34)
61 Open up to C<N> curl connections to the web server.  The default is 4.
62 Curl connections are shared between all NBD clients, so you may wish
63 to increase this if you expect many simultaneous NBD clients (or a
64 single client using many multi-conn connections).
66 See L</NBD CONNECTIONS AND CURL HANDLES> below.
68 =item B<cookie=>COOKIE
70 =item B<cookie=+>FILENAME
72 =item B<cookie=->
74 =item B<cookie=->FD
76 (nbdkit E<ge> 1.12)
78 Set a cookie in the request header when connecting to the remote
79 server.
81 A typical example is:
83  cookie='vmware_soap_session="52a01262-bf93-ccce-d379-8dabb3e55560"'
85 This option can be used at most once.  It only works for HTTP and
86 HTTPS transports.  To set multiple cookies you must concatenate them
87 yourself, eg:
89  cookie='name1=content1; name2=content2'
91 See L<CURLOPT_COOKIE(3)> for more information about this.  The format
92 is quite strict and must consist of C<key=value>, each cookie
93 separated by exactly S<C<"; ">> (semicolon and space).
95 If the cookie is used for authentication then passing it on the
96 command line is not secure on shared machines.  Use the alternate
97 C<+FILENAME> syntax to pass it in a file, C<-> to read the cookie
98 interactively, or C<-FD> to read it from a file descriptor.
100 =item B<cookiefile=>
102 (nbdkit E<ge> 1.26)
104 Enable cookie processing (eg. when following redirects), starting with
105 an empty list of cookies.  This is equivalent to calling
106 L<CURLOPT_COOKIEFILE(3)> with an empty string.
108 =item B<cookiefile=>FILENAME
110 (nbdkit E<ge> 1.26)
112 Enable cookie processing (eg. when following redirects), prepopulating
113 cookies from the given file.  The file can contain cookies in any
114 format supported by curl, see L<CURLOPT_COOKIEFILE(3)>.  Cookies sent
115 by the server are not saved when nbdkit exits.
117 =item B<cookiejar=>FILENAME
119 (nbdkit E<ge> 1.26)
121 Enable cookie processing (eg. when following redirects), prepopulating
122 cookies from the given file, and writing server cookies back to the
123 file when the NBD handle is closed.  The file can contain cookies in
124 any format supported by curl, see L<CURLOPT_COOKIEJAR(3)>.
126 There is some advice on the internet telling you to set this to
127 F</dev/null>, but you B<should not> do this because it can corrupt
128 F</dev/null>.  If you don't want a cookiejar, omit this option.  If
129 you want to enable cookie processing without updating a permanent
130 cookiejar, use the C<cookiefile=> option instead.
132 =item B<cookie-script=>SCRIPT
134 =item B<cookie-script-renew=>SECS
136 (nbdkit E<ge> 1.22, not Windows)
138 Run C<SCRIPT> (a command or shell script fragment) to generate the
139 HTTP/HTTPS cookies.  C<cookie-script> cannot be used with C<cookie>.
140 See L</HEADER AND COOKIE SCRIPTS> below.
142 =item B<followlocation=false>
144 (nbdkit E<ge> 1.26)
146 Do not follow redirects from the server.  The default is true (follow
147 redirects).
149 You can follow redirects but avoid redirecting to a less secure
150 protocol (eg. HTTPS redirecting to FTP) by using the C<protocols>
151 parameter instead.
153 =item B<header=>HEADER
155 (nbdkit E<ge> 1.22)
157 For HTTP/HTTPS, send a custom header, or remove a header that curl has
158 added.  To add a custom header:
160  header='X-My-Name: John Doe'
162 To remove a header that curl has added, add the header followed by a
163 colon and no value:
165  header='User-Agent:'
167 To add a custom header that has no value, you have to use a semicolon
168 instead of colon.  This adds an C<X-Empty:> header with no value:
170  header='X-Empty;'
172 See L<CURLOPT_HTTPHEADER(3)>.  You can use this option multiple times
173 in order to add several headers.  Note this sends the header in all
174 requests, even when following a redirect, which can cause headers
175 (eg. containing sensitive authorization information) to be sent to
176 hosts other than the one originally requested.
178 =item B<header-script=>SCRIPT
180 =item B<header-script-renew=>SECS
182 (nbdkit E<ge> 1.22, not Windows)
184 Run C<SCRIPT> (a command or shell script fragment) to generate the
185 HTTP/HTTPS headers.  C<header-script> cannot be used with C<header>.
186 See L</HEADER AND COOKIE SCRIPTS> below.
188 =item B<http-version=none>
190 =item B<http-version=1.0>
192 =item B<http-version=1.1>
194 =item B<http-version=2.0>
196 =item B<http-version=2TLS>
198 =item B<http-version=2-prior-knowledge>
200 =item B<http-version=3>
202 =item B<http-version=3only>
204 (nbdkit E<ge> 1.34)
206 Force curl to use a particular HTTP protocol.  The default is C<none>
207 meaning curl will negotiate the best protocol with the server.  The
208 other settings are mainly for testing.  See L<CURLOPT_HTTP_VERSION(3)>
209 for details.
211 =item B<password=>PASSWORD
213 Set the password to use when connecting to the remote server.
215 Note that passing this on the command line is not secure on shared
216 machines.
218 =item B<password=->
220 Ask for the password (interactively) when nbdkit starts up.
222 =item B<password=+>FILENAME
224 Read the password from the named file.  This is a secure method
225 to supply a password, as long as you set the permissions on the file
226 appropriately.
228 =item B<password=->FD
230 Read the password from file descriptor number C<FD>, inherited from
231 the parent process when nbdkit starts up.  This is also a secure
232 method to supply a password.
234 =item B<protocols=>PROTO,PROTO,...
236 (nbdkit E<ge> 1.12)
238 Limit the protocols that are allowed in the URL.  Use this option for
239 extra security if the URL comes from an untrusted source and you want
240 to avoid security isues in the more obscure protocols that curl
241 supports.  (See qemu CVE-2013-0249 for an example of a security bug
242 introduced by allowing unrestricted protocols).
244 For example if you only intend HTTP and HTTPS URLs to be used, then
245 add this parameter: C<protocols=http,https>
247 This restriction also applies if the plugin follows a redirect to
248 another protocol (eg. you start with an C<https://> URL which the
249 server redirects to C<ftp://>).  To prevent redirects altogether see
250 the C<followlocation> parameter.
252 The value of this parameter is a comma-separated list of protocols,
253 for example C<protocols=https>, or C<protocols=http,https>, or
254 C<protocols=file,ftp,gopher>.  For a list of known protocols, see the
255 libcurl manual (L<CURLOPT_PROTOCOLS_STR(3)>).
257 The default is to allow any protocol.
259 =item B<proxy=>PROXY
261 (nbdkit E<ge> 1.20)
263 Set the proxy.  See L<CURLOPT_PROXY(3)>.
265 =item B<proxy-password=>PASSWORD
267 =item B<proxy-password=->
269 =item B<proxy-password=+>FILENAME
271 =item B<proxy-password=->FD
273 =item B<proxy-user=>USERNAME
275 (nbdkit E<ge> 1.12)
277 Set the proxy username and password.
279 =item B<sslverify=false>
281 Don't verify the SSL certificate of the remote host.
283 =item B<ssl-cipher-list=>CIPHER[:CIPHER...]
285 =item B<ssl-version=default>
287 =item B<ssl-version=tlsv1>
289 =item B<ssl-version=sslv2>
291 =item B<ssl-version=sslv3>
293 =item B<ssl-version=tlsv1.0>
295 =item B<ssl-version=tlsv1.1>
297 =item B<ssl-version=tlsv1.2>
299 =item B<ssl-version=tlsv1.3>
301 =item B<ssl-version=max-default>
303 =item B<ssl-version=max-tlsv1.0>
305 =item B<ssl-version=max-tlsv1.1>
307 =item B<ssl-version=max-tlsv1.2>
309 =item B<ssl-version=max-tlsv1.3>
311 Set the SSL ciphers and TLS version.  For further information see
312 L<CURLOPT_SSL_CIPHER_LIST(3)> and L<CURLOPT_SSLVERSION(3)>.
314 =item B<tcp-keepalive=true>
316 (nbdkit E<ge> 1.20)
318 Enable TCP keepalives.
320 =item B<tcp-nodelay=false>
322 (nbdkit E<ge> 1.20)
324 Enable Nagle’s algorithm.  Small writes on the network socket will not
325 be sent immediately but will be held in a local buffer and coalesced
326 if possible.  This is more efficient for the network but can cause
327 increased latency.
329 The default (in libcurl E<ge> 7.50.2) is that Nagle’s algorithm is
330 disabled for better latency at the expense of network efficiency.
332 See L<CURLOPT_TCP_NODELAY(3)>.
334 =item B<timeout=>SECS
336 Set the timeout for requests.
338 =item B<timeout=0>
340 Use the default libcurl timeout for requests.
342 =item B<tls13-ciphers=>CIPHER[:CIPHER...]
344 Select TLSv1.3 ciphers available.  See L<CURLOPT_TLS13_CIPHERS(3)> and
345 L<https://curl.se/docs/ssl-ciphers.html>
347 =item B<unix-socket-path=>PATH
349 (nbdkit E<ge> 1.10)
351 Instead of using a TCP connection, connect to the server over the
352 named Unix domain socket.  See L<CURLOPT_UNIX_SOCKET_PATH(3)>.
354 =item [B<url=>]URL
356 The URL of the remote disk image.  This is passed to libcurl directly
357 via L<CURLOPT_URL(3)>.
359 This parameter is required.
361 C<url=> is a magic config key and may be omitted in most cases.
362 See L<nbdkit(1)/Magic parameters>.
364 =item B<user=>USERNAME
366 Set the username to use when connecting to the remote server.  This
367 may also be set in the URL (eg. C<http://foo@example.com/disk.img>)
369 =item B<user-agent=>USER-AGENT
371 (nbdkit E<ge> 1.22)
373 Send user-agent header when using HTTP or HTTPS.  The default is no
374 user-agent header.
376 =back
378 =head1 NBD CONNECTIONS AND CURL HANDLES
380 nbdkit E<le> 1.32 used a simple model where a new NBD connection would
381 create a new libcurl handle.  In practice this meant there was a
382 1-to-1 relationship between NBD connections and HTTP connections to
383 the remote web server (assuming http: or https: URL).
385 nbdkit E<ge> 1.34 changed to using a fixed pool of libcurl handles
386 shared across all NBD connections.  You can control the maximum number
387 of curl handles in the pool with the C<connections> parameter (default
388 4).  Note that if there are more than 4 NBD connections, they will
389 share the 4 web server connections, unless you adjust C<connections>.
391 =head1 HEADER AND COOKIE SCRIPTS
393 While the C<header> and C<cookie> parameters can be used to specify
394 static headers and cookies which are used in every HTTP/HTTPS request,
395 the alternate C<header-script> and C<cookie-script> parameters can be
396 used to run an external script or program to generate headers and/or
397 cookies.  This is particularly useful to access services which require
398 an authorization token.  In addition the C<header-script-renew> and
399 C<cookie-script-renew> parameters allow you to renew the authorization
400 token by rerunning the script periodically.
402 C<header-script> is incompatible with C<header>, and C<cookie-script>
403 is incompatible with C<cookie>.
405 =head2 Header script
407 The header script should print zero or more HTTP headers, each line of
408 output in the same format as the C<header> parameter.  The headers
409 printed by the script are passed to L<CURLOPT_HTTPHEADER(3)>.
411 In the following example, an imaginary web service requires
412 authentication using a token fetched from a separate login server.
413 The token expires after 60 seconds, so we also tell the plugin that it
414 must renew the token (by re-running the script) if more than 50
415 seconds have elapsed since the last request:
417  nbdkit curl https://service.example.com/disk.img \
418         header-script='
419           printf "Authorization: Bearer "
420           curl -s -X POST https://auth.example.com/login |
421                jq -r .token
422         ' \
423         header-script-renew=50
425 =head2 Cookie script
427 The cookie script should print a single line in the same format as the
428 C<cookie> parameter.  This is passed to L<CURLOPT_COOKIE(3)>.
430 =head2 Header and cookie script shell variables
432 Within the C<header-script> and C<cookie-script> the following shell
433 variables are available:
435 =over 4
437 =item C<$iteration>
439 The number of times that the script has been called.  The first time
440 the script is called this contains C<0>.
442 =item C<$url>
444 The URL as passed to the plugin.
446 =back
448 =head2 Example: VMware ESXi cookies
450 VMware ESXi’s web server can expose both VMDK and raw format disk
451 images, but requires you to log in using HTTP Basic Authentication.
452 While you can use the C<user> and C<password> parameters to send HTTP
453 Basic Authentication headers in every request, tests have shown that
454 it is faster to accept the cookie which the server returns and send
455 that instead.  (It is not clear why it is faster, but one theory is
456 that VMware has to do a more expensive username and password check
457 each time.)
459 The web server can be accessed as below.  Since the cookie expires
460 after a certain period of time, we use C<cookie-script-renew>, and
461 because the server uses a self-signed certificate we must use
462 I<--insecure> and C<sslverify=false>.
464  SERVER=esx.example.com
465  DCPATH=data
466  DS=datastore1
467  GUEST=guest-name
468  URL="https://$SERVER/folder/$GUEST/$GUEST-flat.vmdk?dcPath=$DCPATH&dsName=$DS"
470  nbdkit curl "$URL" \
471         cookie-script='
472             curl --head -s --insecure -u root:password "$url" |
473                  sed -ne "{ s/^Set-Cookie: \([^;]*\);.*/\1/ip }"
474         ' \
475         cookie-script-renew=500 \
476         sslverify=false
478 =head2 Example: Docker Hub authorization tokens
480 Accessing objects like container layers from Docker Hub requires that
481 you first fetch an authorization token, even for anonymous access.
482 These tokens expire after about 5 minutes (300 seconds) so must be
483 periodically renewed.
485 You will need this authorization script (F</tmp/auth.sh>):
487  #!/bin/sh -
488  IMAGE=library/fedora
489  curl -s "https://auth.docker.io/token?service=registry.docker.io&scope=repository:$IMAGE:pull" |
490       jq -r .token
492 You will also need this script to get the blobSum of the layer
493 (F</tmp/blobsum.sh>):
495  #!/bin/sh -
496  TOKEN=`/tmp/auth.sh`
497  IMAGE=library/fedora
498  curl -s -X GET -H "Authorization: Bearer $TOKEN" \
499       "https://registry-1.docker.io/v2/$IMAGE/manifests/latest" |
500       jq -r '.fsLayers[0].blobSum'
502 Both scripts must be executable, and both can be run on their own to
503 check they are working.  To run nbdkit:
505  IMAGE=library/fedora
506  BLOBSUM=`/tmp/blobsum.sh`
507  URL="https://registry-1.docker.io/v2/$IMAGE/blobs/$BLOBSUM"
509  nbdkit curl "$URL" \
510         header-script=' printf "Authorization: Bearer "; /tmp/auth.sh ' \
511         header-script-renew=200 \
512         --filter=gzip
514 Note that this exposes a tar file over NBD.  See also
515 L<nbdkit-tar-filter(1)>.
517 =head1 DEBUG FLAGS
519 =over 4
521 =item B<-D curl.scripts=1>
523 This prints out the headers and cookies generated by the
524 C<header-script> and C<cookie-script> options, which can be useful
525 when debugging these scripts.
527 =item B<-D curl.verbose=1>
529 This enables very verbose curl debugging.  See L<CURLOPT_VERBOSE(3)>.
530 This is mainly useful if you suspect there is a bug inside libcurl
531 itself.
533 =back
535 =head1 FILES
537 =over 4
539 =item F<$plugindir/nbdkit-curl-plugin.so>
541 The plugin.
543 Use C<nbdkit --dump-config> to find the location of C<$plugindir>.
545 =back
547 =head1 VERSION
549 C<nbdkit-curl-plugin> first appeared in nbdkit 1.2.
551 =head1 SEE ALSO
553 L<curl(1)>,
554 L<libcurl(3)>,
555 L<CURLOPT_CAINFO(3)>,
556 L<CURLOPT_CAPATH(3)>,
557 L<CURLOPT_COOKIE(3)>,
558 L<CURLOPT_COOKIEFILE(3)>,
559 L<CURLOPT_COOKIEJAR(3)>,
560 L<CURLOPT_FOLLOWLOCATION(3)>,
561 L<CURLOPT_HTTPHEADER(3)>,
562 L<CURLOPT_PROXY(3)>,
563 L<CURLOPT_SSL_CIPHER_LIST(3)>,
564 L<CURLOPT_SSLVERSION(3)>,
565 L<CURLOPT_TCP_KEEPALIVE(3)>,
566 L<CURLOPT_TCP_NODELAY(3)>,
567 L<CURLOPT_TLS13_CIPHERS(3)>,
568 L<CURLOPT_URL(3)>,
569 L<CURLOPT_UNIX_SOCKET_PATH(3)>,
570 L<CURLOPT_USERAGENT(3)>,
571 L<CURLOPT_VERBOSE(3)>,
572 L<nbdkit(1)>,
573 L<nbdkit-extentlist-filter(1)>,
574 L<nbdkit-file-plugin(1)>,
575 L<nbdkit-retry-filter(1)>,
576 L<nbdkit-retry-request-filter(1)>,
577 L<nbdkit-ssh-plugin(1)>,
578 L<nbdkit-torrent-plugin(1)>,
579 L<nbdkit-plugin(3)>,
580 L<http://curl.haxx.se>,
581 L<https://curl.se/docs/ssl-ciphers.html>
583 =head1 AUTHORS
585 Richard W.M. Jones
587 Parts derived from Alexander Graf's "QEMU Block driver for CURL images".
589 =head1 COPYRIGHT
591 Copyright Red Hat