chiark / gitweb /
build: Enable gcc warnings to detect non-portable code.
[gnupg2.git] / dirmngr / t-http.c
1 /* t-http.c
2  * Copyright (C) 1999, 2001, 2002, 2003, 2004, 2006, 2009, 2010,
3  *               2011 Free Software Foundation, Inc.
4  * Copyright (C) 2014 Werner Koch
5  *
6  * This file is part of GnuPG.
7  *
8  * This file is free software; you can redistribute it and/or modify
9  * it under the terms of either
10  *
11  *   - the GNU Lesser General Public License as published by the Free
12  *     Software Foundation; either version 3 of the License, or (at
13  *     your option) any later version.
14  *
15  * or
16  *
17  *   - the GNU General Public License as published by the Free
18  *     Software Foundation; either version 2 of the License, or (at
19  *     your option) any later version.
20  *
21  * or both in parallel, as here.
22  *
23  * This file is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, see <https://www.gnu.org/licenses/>.
30  */
31
32 #include <config.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include <assuan.h>
40
41 #include "util.h"
42 #include "logging.h"
43 #include "http.h"
44
45
46 #if HTTP_USE_NTBTLS
47 # include <ntbtls.h>
48 #elif HTTP_USE_GNUTLS
49 # include <gnutls/gnutls.h>  /* For init, logging, and deinit.  */
50 #endif /*HTTP_USE_GNUTLS*/
51
52 #define PGM "t-http"
53
54 static int verbose;
55 static int debug;
56 static int no_verify;
57
58 /* static void */
59 /* read_dh_params (const char *fname) */
60 /* { */
61 /*   gpg_error_t err; */
62 /*   int rc; */
63 /*   FILE *fp; */
64 /*   struct stat st; */
65 /*   char *buf; */
66 /*   size_t buflen; */
67 /*   gnutls_datum_t datum; */
68
69 /*   fp = fopen (fname, "rb"); */
70 /*   if (!fp) */
71 /*     { */
72 /*       err = gpg_error_from_syserror (); */
73 /*       log_fatal ("can't open '%s': %s\n", fname, gpg_strerror (err)); */
74 /*     } */
75
76 /*   if (fstat (fileno(fp), &st)) */
77 /*     { */
78 /*       err = gpg_error_from_syserror (); */
79 /*       log_fatal ("can't stat '%s': %s\n", fname, gpg_strerror (err)); */
80 /*     } */
81
82 /*   buflen = st.st_size; */
83 /*   buf = xmalloc (buflen+1); */
84 /*   if (fread (buf, buflen, 1, fp) != 1) */
85 /*     { */
86 /*       err = gpg_error_from_syserror (); */
87 /*       log_fatal ("error reading '%s': %s\n", fname, gpg_strerror (err)); */
88 /*     } */
89 /*   fclose (fp); */
90
91 /*   datum.size = buflen; */
92 /*   datum.data = buf; */
93
94 /*   rc = gnutls_dh_params_import_pkcs3 (dh_params, &datum, GNUTLS_X509_FMT_PEM); */
95 /*   if (rc < 0) */
96 /*     log_fatal ("gnutls_dh_param_import failed: %s\n", gnutls_strerror (rc)); */
97
98 /*   xfree (buf); */
99 /* } */
100
101
102
103 #if HTTP_USE_GNUTLS
104 static gpg_error_t
105 verify_callback (http_t hd, http_session_t session, int reserved)
106 {
107   (void)hd;
108   (void)reserved;
109   return no_verify? 0 : http_verify_server_credentials (session);
110 }
111 #endif
112
113 #if HTTP_USE_GNUTLS
114 static void
115 my_gnutls_log (int level, const char *text)
116 {
117   fprintf (stderr, "gnutls:L%d: %s", level, text);
118 }
119 #endif
120
121 /* Prepend FNAME with the srcdir environment variable's value and
122    return an allocated filename. */
123 static char *
124 prepend_srcdir (const char *fname)
125 {
126   static const char *srcdir;
127   char *result;
128
129   if (!srcdir && !(srcdir = getenv ("srcdir")))
130     srcdir = ".";
131
132   result = xmalloc (strlen (srcdir) + 1 + strlen (fname) + 1);
133   strcpy (result, srcdir);
134   strcat (result, "/");
135   strcat (result, fname);
136   return result;
137 }
138
139
140 int
141 main (int argc, char **argv)
142 {
143   int last_argc = -1;
144   gpg_error_t err;
145   int rc;
146   parsed_uri_t uri;
147   uri_tuple_t r;
148   http_t hd;
149   int c;
150   unsigned int my_http_flags = 0;
151   int no_out = 0;
152   int tls_dbg = 0;
153   const char *cafile = NULL;
154   http_session_t session = NULL;
155
156   gpgrt_init ();
157   log_set_prefix (PGM, GPGRT_LOG_WITH_PREFIX | GPGRT_LOG_WITH_PID);
158   if (argc)
159     { argc--; argv++; }
160   while (argc && last_argc != argc )
161     {
162       last_argc = argc;
163       if (!strcmp (*argv, "--"))
164         {
165           argc--; argv++;
166           break;
167         }
168       else if (!strcmp (*argv, "--help"))
169         {
170           fputs ("usage: " PGM " URL\n"
171                  "Options:\n"
172                  "  --verbose         print timings etc.\n"
173                  "  --debug           flyswatter\n"
174                  "  --gnutls-debug N  use GNUTLS debug level N\n"
175                  "  --cacert FNAME    expect CA certificate in file FNAME\n"
176                  "  --no-verify       do not verify the certificate\n"
177                  "  --force-tls       use HTTP_FLAG_FORCE_TLS\n"
178                  "  --force-tor       use HTTP_FLAG_FORCE_TOR\n"
179                  "  --no-out          do not print the content\n",
180                  stdout);
181           exit (0);
182         }
183       else if (!strcmp (*argv, "--verbose"))
184         {
185           verbose++;
186           argc--; argv++;
187         }
188       else if (!strcmp (*argv, "--debug"))
189         {
190           verbose += 2;
191           debug++;
192           argc--; argv++;
193         }
194       else if (!strcmp (*argv, "--gnutls-debug"))
195         {
196           argc--; argv++;
197           if (argc)
198             {
199               tls_dbg = atoi (*argv);
200               argc--; argv++;
201             }
202         }
203       else if (!strcmp (*argv, "--cacert"))
204         {
205           argc--; argv++;
206           if (argc)
207             {
208               cafile = *argv;
209               argc--; argv++;
210             }
211         }
212       else if (!strcmp (*argv, "--no-verify"))
213         {
214           no_verify = 1;
215           argc--; argv++;
216         }
217       else if (!strcmp (*argv, "--force-tls"))
218         {
219           my_http_flags |= HTTP_FLAG_FORCE_TLS;
220           argc--; argv++;
221         }
222       else if (!strcmp (*argv, "--force-tor"))
223         {
224           my_http_flags |= HTTP_FLAG_FORCE_TOR;
225           argc--; argv++;
226         }
227       else if (!strcmp (*argv, "--no-out"))
228         {
229           no_out = 1;
230           argc--; argv++;
231         }
232       else if (!strncmp (*argv, "--", 2))
233         {
234           fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
235           exit (1);
236         }
237     }
238   if (argc != 1)
239     {
240       fprintf (stderr, PGM ": no or too many URLS given\n");
241       exit (1);
242     }
243
244   if (!cafile)
245     cafile = prepend_srcdir ("tls-ca.pem");
246
247   /* http.c makes use of the assuan socket wrapper.  */
248   assuan_sock_init ();
249
250 #if HTTP_USE_NTBTLS
251
252   (void)err;
253
254   ntbtls_set_debug (tls_dbg, NULL, NULL);
255
256 #elif HTTP_USE_GNUTLS
257
258   rc = gnutls_global_init ();
259   if (rc)
260     log_error ("gnutls_global_init failed: %s\n", gnutls_strerror (rc));
261
262   http_register_tls_callback (verify_callback);
263   http_register_tls_ca (cafile);
264
265   err = http_session_new (&session, NULL, NULL, HTTP_FLAG_TRUST_DEF);
266   if (err)
267     log_error ("http_session_new failed: %s\n", gpg_strerror (err));
268
269   /* rc = gnutls_dh_params_init(&dh_params); */
270   /* if (rc) */
271   /*   log_error ("gnutls_dh_params_init failed: %s\n", gnutls_strerror (rc)); */
272   /* read_dh_params ("dh_param.pem"); */
273
274   /* rc = gnutls_certificate_set_x509_trust_file */
275   /*   (certcred, "ca.pem", GNUTLS_X509_FMT_PEM); */
276   /* if (rc) */
277   /*   log_error ("gnutls_certificate_set_x509_trust_file failed: %s\n", */
278   /*              gnutls_strerror (rc)); */
279
280   /* gnutls_certificate_set_dh_params (certcred, dh_params); */
281
282   gnutls_global_set_log_function (my_gnutls_log);
283   if (tls_dbg)
284     gnutls_global_set_log_level (tls_dbg);
285
286 #endif /*HTTP_USE_GNUTLS*/
287
288   rc = http_parse_uri (&uri, *argv, 1);
289   if (rc)
290     {
291       log_error ("'%s': %s\n", *argv, gpg_strerror (rc));
292       return 1;
293     }
294
295   printf ("Scheme: %s\n", uri->scheme);
296   if (uri->opaque)
297     printf ("Value : %s\n", uri->path);
298   else
299     {
300       printf ("Auth  : %s\n", uri->auth? uri->auth:"[none]");
301       printf ("Host  : %s\n", uri->host);
302       printf ("Port  : %u\n", uri->port);
303       printf ("Path  : %s\n", uri->path);
304       for (r = uri->params; r; r = r->next)
305         {
306           printf ("Params: %s", r->name);
307           if (!r->no_value)
308             {
309               printf ("=%s", r->value);
310               if (strlen (r->value) != r->valuelen)
311                 printf (" [real length=%d]", (int) r->valuelen);
312             }
313           putchar ('\n');
314         }
315       for (r = uri->query; r; r = r->next)
316         {
317           printf ("Query : %s", r->name);
318           if (!r->no_value)
319             {
320               printf ("=%s", r->value);
321               if (strlen (r->value) != r->valuelen)
322                 printf (" [real length=%d]", (int) r->valuelen);
323             }
324           putchar ('\n');
325         }
326       printf ("Flags :%s%s%s%s\n",
327               uri->is_http? " http":"",
328               uri->opaque?  " opaque":"",
329               uri->v6lit?   " v6lit":"",
330               uri->onion?   " onion":"");
331       printf ("TLS   : %s\n",
332               uri->use_tls? "yes":
333               (my_http_flags&HTTP_FLAG_FORCE_TLS)? "forced" : "no");
334       printf ("Tor   : %s\n",
335               (my_http_flags&HTTP_FLAG_FORCE_TOR)? "yes" : "no");
336
337     }
338   fflush (stdout);
339   http_release_parsed_uri (uri);
340   uri = NULL;
341
342   rc = http_open_document (&hd, *argv, NULL, my_http_flags,
343                            NULL, session, NULL, NULL);
344   if (rc)
345     {
346       log_error ("can't get '%s': %s\n", *argv, gpg_strerror (rc));
347       return 1;
348     }
349   log_info ("open_http_document succeeded; status=%u\n",
350             http_get_status_code (hd));
351
352   {
353     const char **names;
354     int i;
355
356     names = http_get_header_names (hd);
357     if (!names)
358       log_fatal ("http_get_header_names failed: %s\n",
359                  gpg_strerror (gpg_error_from_syserror ()));
360     for (i = 0; names[i]; i++)
361       printf ("HDR: %s: %s\n", names[i], http_get_header (hd, names[i]));
362     xfree (names);
363   }
364   fflush (stdout);
365
366   switch (http_get_status_code (hd))
367     {
368     case 200:
369     case 400:
370     case 401:
371     case 403:
372     case 404:
373       {
374         unsigned long count = 0;
375         while ((c = es_getc (http_get_read_ptr (hd))) != EOF)
376           {
377             count++;
378             if (!no_out)
379               putchar (c);
380           }
381         log_info ("Received bytes: %lu\n", count);
382       }
383       break;
384     case 301:
385     case 302:
386     case 307:
387       log_info ("Redirected to: %s\n", http_get_header (hd, "Location"));
388       break;
389     }
390   http_close (hd, 0);
391
392   http_session_release (session);
393 #ifdef HTTP_USE_GNUTLS
394   gnutls_global_deinit ();
395 #endif /*HTTP_USE_GNUTLS*/
396
397   return 0;
398 }