xref: /freebsd/lib/libfetch/common.h (revision 4db3872aabc33088cf180599c5eaa23b6f58e6d1)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1998-2014 Dag-Erling Smørgrav
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef _COMMON_H_INCLUDED
32 #define _COMMON_H_INCLUDED
33 
34 #define FTP_DEFAULT_PORT	21
35 #define HTTP_DEFAULT_PORT	80
36 #define FTP_DEFAULT_PROXY_PORT	21
37 #define HTTP_DEFAULT_PROXY_PORT	3128
38 
39 #ifdef WITH_SSL
40 #include <openssl/crypto.h>
41 #include <openssl/x509.h>
42 #include <openssl/pem.h>
43 #include <openssl/ssl.h>
44 #include <openssl/err.h>
45 #endif
46 
47 /* Connection */
48 typedef struct fetchconn conn_t;
49 struct fetchconn {
50 	int		 sd;		/* socket descriptor */
51 	char		*buf;		/* buffer */
52 	char		*line;		/* line (within buffer) */
53 	size_t		 bufsize;	/* buffer size */
54 	size_t		 buflen;	/* length of buffer contents */
55 	size_t		 linelen;	/* length of line */
56 	size_t		 pos;		/* current position in buffer */
57 	int		 err;		/* last protocol reply code */
58 #ifdef WITH_SSL
59 	SSL		*ssl;		/* SSL handle */
60 	SSL_CTX		*ssl_ctx;	/* SSL context */
61 	X509		*ssl_cert;	/* server certificate */
62 #endif
63 	int		 ref;		/* reference count */
64 };
65 
66 static inline conn_t *fetch_ref(conn_t *conn)
67 {
68 	++conn->ref;
69 	return (conn);
70 }
71 
72 static inline conn_t *fetch_deref(conn_t *conn)
73 {
74 	--conn->ref;
75 	return (conn);
76 }
77 
78 /* Structure used for error message lists */
79 struct fetcherr {
80 	const int	 num;
81 	const int	 cat;
82 	const char	*string;
83 };
84 
85 /* For SOCKS header size */
86 #define HEAD_SIZE	4
87 #define FQDN_SIZE	256
88 #define PACK_SIZE	1
89 #define PORT_SIZE	2
90 #define BUFF_SIZE	HEAD_SIZE + FQDN_SIZE + PACK_SIZE + PORT_SIZE
91 
92 /* SOCKS5 Request Header */
93 #define SOCKS_VERSION_5		0x05
94 /* SOCKS5 CMD */
95 #define SOCKS_CONNECTION	0x01
96 #define SOCKS_BIND		0x02
97 #define SOCKS_UDP		0x03
98 #define SOCKS_NOMETHODS		0xFF
99 #define SOCKS5_NOTIMPLEMENTED	0x00
100 /* SOCKS5 Reserved */
101 #define SOCKS_RSV		0x00
102 /* SOCKS5 Address Type */
103 #define SOCKS_ATYP_IPV4		0x01
104 #define SOCKS_ATYP_DOMAINNAME	0x03
105 #define SOCKS_ATYP_IPV6		0x04
106 /* SOCKS5 Reply Field */
107 #define SOCKS_SUCCESS			0x00
108 #define SOCKS_GENERAL_FAILURE		0x01
109 #define SOCKS_CONNECTION_NOT_ALLOWED	0x02
110 #define SOCKS_NETWORK_UNREACHABLE	0x03
111 #define SOCKS_HOST_UNREACHABLE		0x04
112 #define SOCKS_CONNECTION_REFUSED	0x05
113 #define SOCKS_TTL_EXPIRED		0x06
114 #define SOCKS_COMMAND_NOT_SUPPORTED	0x07
115 #define SOCKS_ADDRESS_NOT_SUPPORTED	0x08
116 
117 /* for fetch_writev */
118 struct iovec;
119 
120 void		 fetch_seterr(struct fetcherr *, int);
121 void		 fetch_syserr(void);
122 void		 fetch_info(const char *, ...) __printflike(1, 2);
123 int		 fetch_socks5_getenv(char **host, int *port);
124 int		 fetch_socks5_init(conn_t *conn, const char *host,
125 		     int port, int verbose);
126 int		 fetch_default_port(const char *);
127 int		 fetch_default_proxy_port(const char *);
128 struct addrinfo *fetch_resolve(const char *, int, int);
129 int		 fetch_bind(int, int, const char *);
130 conn_t		*fetch_connect(const char *, int, int, int);
131 conn_t		*fetch_reopen(int);
132 #ifdef WITH_SSL
133 int		 fetch_ssl_cb_verify_crt(int, X509_STORE_CTX*);
134 #endif
135 int		 fetch_ssl(conn_t *, const struct url *, int);
136 ssize_t		 fetch_read(conn_t *, void *, size_t);
137 ssize_t		 fetch_getln(conn_t *);
138 ssize_t		 fetch_bufread(conn_t *, void *, size_t);
139 ssize_t		 fetch_write(conn_t *, const void *, size_t);
140 ssize_t		 fetch_writev(conn_t *, struct iovec *, int);
141 int		 fetch_putln(conn_t *, const char *, size_t);
142 int		 fetch_close(conn_t *);
143 int		 fetch_add_entry(struct url_ent **, int *, int *,
144 		     const char *, struct url_stat *);
145 int		 fetch_netrc_auth(struct url *url);
146 int		 fetch_no_proxy_match(const char *);
147 
148 #define ftp_seterr(n)	 fetch_seterr(ftp_errlist, n)
149 #define http_seterr(n)	 fetch_seterr(http_errlist, n)
150 #define netdb_seterr(n)	 fetch_seterr(netdb_errlist, n)
151 #define url_seterr(n)	 fetch_seterr(url_errlist, n)
152 #define socks5_seterr(n) fetch_seterr(socks5_errlist, n)
153 
154 #ifndef NDEBUG
155 #define DEBUGF(...)							\
156 	do {								\
157 		if (fetchDebug)						\
158 			fprintf(stderr, __VA_ARGS__);			\
159 	} while (0)
160 #else
161 #define DEBUGF(...)							\
162 	do {								\
163 		/* nothing */						\
164 	} while (0)
165 #endif
166 
167 /*
168  * I don't really like exporting http_request() and ftp_request(),
169  * but the HTTP and FTP code occasionally needs to cross-call
170  * eachother, and this saves me from adding a lot of special-case code
171  * to handle those cases.
172  *
173  * Note that _*_request() free purl, which is way ugly but saves us a
174  * whole lot of trouble.
175  */
176 FILE		*http_request(struct url *, const char *,
177 		     struct url_stat *, struct url *, const char *);
178 FILE		*http_request_body(struct url *, const char *,
179 		     struct url_stat *, struct url *, const char *,
180 		     const char *, const char *);
181 FILE		*ftp_request(struct url *, const char *,
182 		     struct url_stat *, struct url *, const char *);
183 
184 /*
185  * Check whether a particular flag is set
186  */
187 #define CHECK_FLAG(x)	(flags && strchr(flags, (x)))
188 
189 #endif
190