xref: /freebsd/lib/libfetch/fetch.3 (revision 63dab8eed99114670445270e26cf7193fe55e0fa)
1.\"-
2.\" Copyright (c) 1998-2011 Dag-Erling Smørgrav
3.\" All rights reserved.
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\"    notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\"    notice, this list of conditions and the following disclaimer in the
12.\"    documentation and/or other materials provided with the distribution.
13.\"
14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24.\" SUCH DAMAGE.
25.\"
26.\" $FreeBSD$
27.\"
28.Dd September 27, 2011
29.Dt FETCH 3
30.Os
31.Sh NAME
32.Nm fetchMakeURL ,
33.Nm fetchParseURL ,
34.Nm fetchFreeURL ,
35.Nm fetchXGetURL ,
36.Nm fetchGetURL ,
37.Nm fetchPutURL ,
38.Nm fetchStatURL ,
39.Nm fetchListURL ,
40.Nm fetchXGet ,
41.Nm fetchGet ,
42.Nm fetchPut ,
43.Nm fetchStat ,
44.Nm fetchList ,
45.Nm fetchXGetFile ,
46.Nm fetchGetFile ,
47.Nm fetchPutFile ,
48.Nm fetchStatFile ,
49.Nm fetchListFile ,
50.Nm fetchXGetHTTP ,
51.Nm fetchGetHTTP ,
52.Nm fetchPutHTTP ,
53.Nm fetchStatHTTP ,
54.Nm fetchListHTTP ,
55.Nm fetchXGetFTP ,
56.Nm fetchGetFTP ,
57.Nm fetchPutFTP ,
58.Nm fetchStatFTP ,
59.Nm fetchListFTP
60.Nd file transfer functions
61.Sh LIBRARY
62.Lb libfetch
63.Sh SYNOPSIS
64.In sys/param.h
65.In stdio.h
66.In fetch.h
67.Ft struct url *
68.Fn fetchMakeURL "const char *scheme" "const char *host" "int port" "const char *doc" "const char *user" "const char *pwd"
69.Ft struct url *
70.Fn fetchParseURL "const char *URL"
71.Ft void
72.Fn fetchFreeURL "struct url *u"
73.Ft FILE *
74.Fn fetchXGetURL "const char *URL" "struct url_stat *us" "const char *flags"
75.Ft FILE *
76.Fn fetchGetURL "const char *URL" "const char *flags"
77.Ft FILE *
78.Fn fetchPutURL "const char *URL" "const char *flags"
79.Ft int
80.Fn fetchStatURL "const char *URL" "struct url_stat *us" "const char *flags"
81.Ft struct url_ent *
82.Fn fetchListURL "const char *URL" "const char *flags"
83.Ft FILE *
84.Fn fetchXGet "struct url *u" "struct url_stat *us" "const char *flags"
85.Ft FILE *
86.Fn fetchGet "struct url *u" "const char *flags"
87.Ft FILE *
88.Fn fetchPut "struct url *u" "const char *flags"
89.Ft int
90.Fn fetchStat "struct url *u" "struct url_stat *us" "const char *flags"
91.Ft struct url_ent *
92.Fn fetchList "struct url *u" "const char *flags"
93.Ft FILE *
94.Fn fetchXGetFile "struct url *u" "struct url_stat *us" "const char *flags"
95.Ft FILE *
96.Fn fetchGetFile "struct url *u" "const char *flags"
97.Ft FILE *
98.Fn fetchPutFile "struct url *u" "const char *flags"
99.Ft int
100.Fn fetchStatFile "struct url *u" "struct url_stat *us" "const char *flags"
101.Ft struct url_ent *
102.Fn fetchListFile "struct url *u" "const char *flags"
103.Ft FILE *
104.Fn fetchXGetHTTP "struct url *u" "struct url_stat *us" "const char *flags"
105.Ft FILE *
106.Fn fetchGetHTTP "struct url *u" "const char *flags"
107.Ft FILE *
108.Fn fetchPutHTTP "struct url *u" "const char *flags"
109.Ft int
110.Fn fetchStatHTTP "struct url *u" "struct url_stat *us" "const char *flags"
111.Ft struct url_ent *
112.Fn fetchListHTTP "struct url *u" "const char *flags"
113.Ft FILE *
114.Fn fetchXGetFTP "struct url *u" "struct url_stat *us" "const char *flags"
115.Ft FILE *
116.Fn fetchGetFTP "struct url *u" "const char *flags"
117.Ft FILE *
118.Fn fetchPutFTP "struct url *u" "const char *flags"
119.Ft int
120.Fn fetchStatFTP "struct url *u" "struct url_stat *us" "const char *flags"
121.Ft struct url_ent *
122.Fn fetchListFTP "struct url *u" "const char *flags"
123.Sh DESCRIPTION
124These functions implement a high-level library for retrieving and
125uploading files using Uniform Resource Locators (URLs).
126.Pp
127.Fn fetchParseURL
128takes a URL in the form of a null-terminated string and splits it into
129its components function according to the Common Internet Scheme Syntax
130detailed in RFC1738.
131A regular expression which produces this syntax is:
132.Bd -literal
133    <scheme>:(//(<user>(:<pwd>)?@)?<host>(:<port>)?)?/(<document>)?
134.Ed
135.Pp
136If the URL does not seem to begin with a scheme name, the following
137syntax is assumed:
138.Bd -literal
139    ((<user>(:<pwd>)?@)?<host>(:<port>)?)?/(<document>)?
140.Ed
141.Pp
142Note that some components of the URL are not necessarily relevant to
143all URL schemes.
144For instance, the file scheme only needs the <scheme> and <document>
145components.
146.Pp
147.Fn fetchMakeURL
148and
149.Fn fetchParseURL
150return a pointer to a
151.Vt url
152structure, which is defined as follows in
153.In fetch.h :
154.Bd -literal
155#define URL_SCHEMELEN 16
156#define URL_USERLEN 256
157#define URL_PWDLEN 256
158
159struct url {
160    char	 scheme[URL_SCHEMELEN+1];
161    char	 user[URL_USERLEN+1];
162    char	 pwd[URL_PWDLEN+1];
163    char	 host[MAXHOSTNAMELEN+1];
164    int		 port;
165    char	*doc;
166    off_t	 offset;
167    size_t	 length;
168    time_t	 ims_time;
169};
170.Ed
171.Pp
172The
173.Va ims_time
174field stores the time value for
175.Li If-Modified-Since
176HTTP requests.
177.Pp
178The pointer returned by
179.Fn fetchMakeURL
180or
181.Fn fetchParseURL
182should be freed using
183.Fn fetchFreeURL .
184.Pp
185.Fn fetchXGetURL ,
186.Fn fetchGetURL ,
187and
188.Fn fetchPutURL
189constitute the recommended interface to the
190.Nm fetch
191library.
192They examine the URL passed to them to determine the transfer
193method, and call the appropriate lower-level functions to perform the
194actual transfer.
195.Fn fetchXGetURL
196also returns the remote document's metadata in the
197.Vt url_stat
198structure pointed to by the
199.Fa us
200argument.
201.Pp
202The
203.Fa flags
204argument is a string of characters which specify transfer options.
205The
206meaning of the individual flags is scheme-dependent, and is detailed
207in the appropriate section below.
208.Pp
209.Fn fetchStatURL
210attempts to obtain the requested document's metadata and fill in the
211structure pointed to by its second argument.
212The
213.Vt url_stat
214structure is defined as follows in
215.In fetch.h :
216.Bd -literal
217struct url_stat {
218    off_t	 size;
219    time_t	 atime;
220    time_t	 mtime;
221};
222.Ed
223.Pp
224If the size could not be obtained from the server, the
225.Fa size
226field is set to -1.
227If the modification time could not be obtained from the server, the
228.Fa mtime
229field is set to the epoch.
230If the access time could not be obtained from the server, the
231.Fa atime
232field is set to the modification time.
233.Pp
234.Fn fetchListURL
235attempts to list the contents of the directory pointed to by the URL
236provided.
237If successful, it returns a malloced array of
238.Vt url_ent
239structures.
240The
241.Vt url_ent
242structure is defined as follows in
243.In fetch.h :
244.Bd -literal
245struct url_ent {
246    char         name[PATH_MAX];
247    struct url_stat stat;
248};
249.Ed
250.Pp
251The list is terminated by an entry with an empty name.
252.Pp
253The pointer returned by
254.Fn fetchListURL
255should be freed using
256.Fn free .
257.Pp
258.Fn fetchXGet ,
259.Fn fetchGet ,
260.Fn fetchPut
261and
262.Fn fetchStat
263are similar to
264.Fn fetchXGetURL ,
265.Fn fetchGetURL ,
266.Fn fetchPutURL
267and
268.Fn fetchStatURL ,
269except that they expect a pre-parsed URL in the form of a pointer to
270a
271.Vt struct url
272rather than a string.
273.Pp
274All of the
275.Fn fetchXGetXXX ,
276.Fn fetchGetXXX
277and
278.Fn fetchPutXXX
279functions return a pointer to a stream which can be used to read or
280write data from or to the requested document, respectively.
281Note that
282although the implementation details of the individual access methods
283vary, it can generally be assumed that a stream returned by one of the
284.Fn fetchXGetXXX
285or
286.Fn fetchGetXXX
287functions is read-only, and that a stream returned by one of the
288.Fn fetchPutXXX
289functions is write-only.
290.Sh FILE SCHEME
291.Fn fetchXGetFile ,
292.Fn fetchGetFile
293and
294.Fn fetchPutFile
295provide access to documents which are files in a locally mounted file
296system.
297Only the <document> component of the URL is used.
298.Pp
299.Fn fetchXGetFile
300and
301.Fn fetchGetFile
302do not accept any flags.
303.Pp
304.Fn fetchPutFile
305accepts the
306.Ql a
307(append to file) flag.
308If that flag is specified, the data written to
309the stream returned by
310.Fn fetchPutFile
311will be appended to the previous contents of the file, instead of
312replacing them.
313.Sh FTP SCHEME
314.Fn fetchXGetFTP ,
315.Fn fetchGetFTP
316and
317.Fn fetchPutFTP
318implement the FTP protocol as described in RFC959.
319.Pp
320If the
321.Ql P
322(not passive) flag is specified, an active (rather than passive)
323connection will be attempted.
324.Pp
325The
326.Ql p
327flag is supported for compatibility with earlier versions where active
328connections were the default.
329It has precedence over the
330.Ql P
331flag, so if both are specified,
332.Nm
333will use a passive connection.
334.Pp
335If the
336.Ql l
337(low) flag is specified, data sockets will be allocated in the low (or
338default) port range instead of the high port range (see
339.Xr ip 4 ) .
340.Pp
341If the
342.Ql d
343(direct) flag is specified,
344.Fn fetchXGetFTP ,
345.Fn fetchGetFTP
346and
347.Fn fetchPutFTP
348will use a direct connection even if a proxy server is defined.
349.Pp
350If no user name or password is given, the
351.Nm fetch
352library will attempt an anonymous login, with user name "anonymous"
353and password "anonymous@<hostname>".
354.Sh HTTP SCHEME
355The
356.Fn fetchXGetHTTP ,
357.Fn fetchGetHTTP
358and
359.Fn fetchPutHTTP
360functions implement the HTTP/1.1 protocol.
361With a little luck, there is
362even a chance that they comply with RFC2616 and RFC2617.
363.Pp
364If the
365.Ql d
366(direct) flag is specified,
367.Fn fetchXGetHTTP ,
368.Fn fetchGetHTTP
369and
370.Fn fetchPutHTTP
371will use a direct connection even if a proxy server is defined.
372.Pp
373If the
374.Ql i
375(if-modified-since) flag is specified, and
376the
377.Va ims_time
378field is set in
379.Vt "struct url" ,
380then
381.Fn fetchXGetHTTP
382and
383.Fn fetchGetHTTP
384will send a conditional
385.Li If-Modified-Since
386HTTP header to only fetch the content if it is newer than
387.Va ims_time .
388.Pp
389Since there seems to be no good way of implementing the HTTP PUT
390method in a manner consistent with the rest of the
391.Nm fetch
392library,
393.Fn fetchPutHTTP
394is currently unimplemented.
395.Sh AUTHENTICATION
396Apart from setting the appropriate environment variables and
397specifying the user name and password in the URL or the
398.Vt struct url ,
399the calling program has the option of defining an authentication
400function with the following prototype:
401.Pp
402.Ft int
403.Fn myAuthMethod "struct url *u"
404.Pp
405The callback function should fill in the
406.Fa user
407and
408.Fa pwd
409fields in the provided
410.Vt struct url
411and return 0 on success, or any other value to indicate failure.
412.Pp
413To register the authentication callback, simply set
414.Va fetchAuthMethod
415to point at it.
416The callback will be used whenever a site requires authentication and
417the appropriate environment variables are not set.
418.Pp
419This interface is experimental and may be subject to change.
420.Sh RETURN VALUES
421.Fn fetchParseURL
422returns a pointer to a
423.Vt struct url
424containing the individual components of the URL.
425If it is
426unable to allocate memory, or the URL is syntactically incorrect,
427.Fn fetchParseURL
428returns a NULL pointer.
429.Pp
430The
431.Fn fetchStat
432functions return 0 on success and -1 on failure.
433.Pp
434All other functions return a stream pointer which may be used to
435access the requested document, or NULL if an error occurred.
436.Pp
437The following error codes are defined in
438.In fetch.h :
439.Bl -tag -width 18n
440.It Bq Er FETCH_ABORT
441Operation aborted
442.It Bq Er FETCH_AUTH
443Authentication failed
444.It Bq Er FETCH_DOWN
445Service unavailable
446.It Bq Er FETCH_EXISTS
447File exists
448.It Bq Er FETCH_FULL
449File system full
450.It Bq Er FETCH_INFO
451Informational response
452.It Bq Er FETCH_MEMORY
453Insufficient memory
454.It Bq Er FETCH_MOVED
455File has moved
456.It Bq Er FETCH_NETWORK
457Network error
458.It Bq Er FETCH_OK
459No error
460.It Bq Er FETCH_PROTO
461Protocol error
462.It Bq Er FETCH_RESOLV
463Resolver error
464.It Bq Er FETCH_SERVER
465Server error
466.It Bq Er FETCH_TEMP
467Temporary error
468.It Bq Er FETCH_TIMEOUT
469Operation timed out
470.It Bq Er FETCH_UNAVAIL
471File is not available
472.It Bq Er FETCH_UNKNOWN
473Unknown error
474.It Bq Er FETCH_URL
475Invalid URL
476.El
477.Pp
478The accompanying error message includes a protocol-specific error code
479and message, e.g.\& "File is not available (404 Not Found)"
480.Sh ENVIRONMENT
481.Bl -tag -width ".Ev FETCH_BIND_ADDRESS"
482.It Ev FETCH_BIND_ADDRESS
483Specifies a hostname or IP address to which sockets used for outgoing
484connections will be bound.
485.It Ev FTP_LOGIN
486Default FTP login if none was provided in the URL.
487.It Ev FTP_PASSIVE_MODE
488If set to
489.Ql no ,
490forces the FTP code to use active mode.
491If set to any other value, forces passive mode even if the application
492requested active mode.
493.It Ev FTP_PASSWORD
494Default FTP password if the remote server requests one and none was
495provided in the URL.
496.It Ev FTP_PROXY
497URL of the proxy to use for FTP requests.
498The document part is ignored.
499FTP and HTTP proxies are supported; if no scheme is specified, FTP is
500assumed.
501If the proxy is an FTP proxy,
502.Nm libfetch
503will send
504.Ql user@host
505as user name to the proxy, where
506.Ql user
507is the real user name, and
508.Ql host
509is the name of the FTP server.
510.Pp
511If this variable is set to an empty string, no proxy will be used for
512FTP requests, even if the
513.Ev HTTP_PROXY
514variable is set.
515.It Ev ftp_proxy
516Same as
517.Ev FTP_PROXY ,
518for compatibility.
519.It Ev HTTP_AUTH
520Specifies HTTP authorization parameters as a colon-separated list of
521items.
522The first and second item are the authorization scheme and realm
523respectively; further items are scheme-dependent.
524Currently, the
525.Dq basic
526and
527.Dq digest
528authorization methods are supported.
529.Pp
530Both methods require two parameters: the user name and
531password, in that order.
532.Pp
533This variable is only used if the server requires authorization and
534no user name or password was specified in the URL.
535.It Ev HTTP_PROXY
536URL of the proxy to use for HTTP requests.
537The document part is ignored.
538Only HTTP proxies are supported for HTTP requests.
539If no port number is specified, the default is 3128.
540.Pp
541Note that this proxy will also be used for FTP documents, unless the
542.Ev FTP_PROXY
543variable is set.
544.It Ev http_proxy
545Same as
546.Ev HTTP_PROXY ,
547for compatibility.
548.It Ev HTTP_PROXY_AUTH
549Specifies authorization parameters for the HTTP proxy in the same
550format as the
551.Ev HTTP_AUTH
552variable.
553.Pp
554This variable is used if and only if connected to an HTTP proxy, and
555is ignored if a user and/or a password were specified in the proxy
556URL.
557.It Ev HTTP_REFERER
558Specifies the referrer URL to use for HTTP requests.
559If set to
560.Dq auto ,
561the document URL will be used as referrer URL.
562.It Ev HTTP_USER_AGENT
563Specifies the User-Agent string to use for HTTP requests.
564This can be useful when working with HTTP origin or proxy servers that
565differentiate between user agents.
566.It Ev NETRC
567Specifies a file to use instead of
568.Pa ~/.netrc
569to look up login names and passwords for FTP sites.
570See
571.Xr ftp 1
572for a description of the file format.
573This feature is experimental.
574.It Ev NO_PROXY
575Either a single asterisk, which disables the use of proxies
576altogether, or a comma- or whitespace-separated list of hosts for
577which proxies should not be used.
578.It Ev no_proxy
579Same as
580.Ev NO_PROXY ,
581for compatibility.
582.El
583.Sh EXAMPLES
584To access a proxy server on
585.Pa proxy.example.com
586port 8080, set the
587.Ev HTTP_PROXY
588environment variable in a manner similar to this:
589.Pp
590.Dl HTTP_PROXY=http://proxy.example.com:8080
591.Pp
592If the proxy server requires authentication, there are
593two options available for passing the authentication data.
594The first method is by using the proxy URL:
595.Pp
596.Dl HTTP_PROXY=http://<user>:<pwd>@proxy.example.com:8080
597.Pp
598The second method is by using the
599.Ev HTTP_PROXY_AUTH
600environment variable:
601.Bd -literal -offset indent
602HTTP_PROXY=http://proxy.example.com:8080
603HTTP_PROXY_AUTH=basic:*:<user>:<pwd>
604.Ed
605.Pp
606To disable the use of a proxy for an HTTP server running on the local
607host, define
608.Ev NO_PROXY
609as follows:
610.Bd -literal -offset indent
611NO_PROXY=localhost,127.0.0.1
612.Ed
613.Sh SEE ALSO
614.Xr fetch 1 ,
615.Xr ftpio 3 ,
616.Xr ip 4
617.Rs
618.%A J. Postel
619.%A J. K. Reynolds
620.%D October 1985
621.%B File Transfer Protocol
622.%O RFC959
623.Re
624.Rs
625.%A P. Deutsch
626.%A A. Emtage
627.%A A. Marine.
628.%D May 1994
629.%T How to Use Anonymous FTP
630.%O RFC1635
631.Re
632.Rs
633.%A T. Berners-Lee
634.%A L. Masinter
635.%A M. McCahill
636.%D December 1994
637.%T Uniform Resource Locators (URL)
638.%O RFC1738
639.Re
640.Rs
641.%A R. Fielding
642.%A J. Gettys
643.%A J. Mogul
644.%A H. Frystyk
645.%A L. Masinter
646.%A P. Leach
647.%A T. Berners-Lee
648.%D January 1999
649.%B Hypertext Transfer Protocol -- HTTP/1.1
650.%O RFC2616
651.Re
652.Rs
653.%A J. Franks
654.%A P. Hallam-Baker
655.%A J. Hostetler
656.%A S. Lawrence
657.%A P. Leach
658.%A A. Luotonen
659.%A L. Stewart
660.%D June 1999
661.%B HTTP Authentication: Basic and Digest Access Authentication
662.%O RFC2617
663.Re
664.Sh HISTORY
665The
666.Nm fetch
667library first appeared in
668.Fx 3.0 .
669.Sh AUTHORS
670.An -nosplit
671The
672.Nm fetch
673library was mostly written by
674.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org
675with numerous suggestions and contributions from
676.An Jordan K. Hubbard Aq jkh@FreeBSD.org ,
677.An Eugene Skepner Aq eu@qub.com ,
678.An Hajimu Umemoto Aq ume@FreeBSD.org ,
679.An Henry Whincup Aq henry@techiebod.com ,
680.An Jukka A. Ukkonen Aq jau@iki.fi ,
681.An Jean-Fran\(,cois Dockes Aq jf@dockes.org
682and others.
683It replaces the older
684.Nm ftpio
685library written by
686.An Poul-Henning Kamp Aq phk@FreeBSD.org
687and
688.An Jordan K. Hubbard Aq jkh@FreeBSD.org .
689.Pp
690This manual page was written by
691.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .
692.Sh BUGS
693Some parts of the library are not yet implemented.
694The most notable
695examples of this are
696.Fn fetchPutHTTP ,
697.Fn fetchListHTTP ,
698.Fn fetchListFTP
699and FTP proxy support.
700.Pp
701There is no way to select a proxy at run-time other than setting the
702.Ev HTTP_PROXY
703or
704.Ev FTP_PROXY
705environment variables as appropriate.
706.Pp
707.Nm libfetch
708does not understand or obey 305 (Use Proxy) replies.
709.Pp
710Error numbers are unique only within a certain context; the error
711codes used for FTP and HTTP overlap, as do those used for resolver and
712system errors.
713For instance, error code 202 means "Command not
714implemented, superfluous at this site" in an FTP context and
715"Accepted" in an HTTP context.
716.Pp
717.Fn fetchStatFTP
718does not check that the result of an MDTM command is a valid date.
719.Pp
720The man page is incomplete, poorly written and produces badly
721formatted text.
722.Pp
723The error reporting mechanism is unsatisfactory.
724.Pp
725Some parts of the code are not fully reentrant.
726