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