chiark / gitweb /
dirmngr: New option --no-use-tor and internal changes.
[gnupg2.git] / dirmngr / ks-engine-finger.c
1 /* ks-engine-finger.c - Finger OpenPGP key access
2  * Copyright (C) 2011 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <https://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
26
27 #include "dirmngr.h"
28 #include "misc.h"
29 #include "userids.h"
30 #include "ks-engine.h"
31
32 /* Print a help output for the schemata supported by this module. */
33 gpg_error_t
34 ks_finger_help (ctrl_t ctrl, parsed_uri_t uri)
35 {
36   char const data[] =
37     "Handler for FINGER:\n"
38     "  finger:<user>@<host>\n"
39     "Supported methods: fetch\n"
40     "Example:\n"
41     "  finger:joe@example.org\n";
42   gpg_error_t err;
43
44   if (!uri)
45     err = ks_print_help (ctrl, "  finger");
46   else if (!strcmp (uri->scheme, "finger"))
47     err = ks_print_help (ctrl, data);
48   else
49     err = 0;
50
51   return err;
52 }
53
54
55 /* Get the key from URI which is expected to specify a finger scheme.
56    On success R_FP has an open stream to read the data.  */
57 gpg_error_t
58 ks_finger_fetch (ctrl_t ctrl, parsed_uri_t uri, estream_t *r_fp)
59 {
60   gpg_error_t err;
61   estream_t fp;
62   char *server;
63   char *name;
64   http_t http;
65
66   (void)ctrl;
67   *r_fp = NULL;
68
69   if (strcmp (uri->scheme, "finger") || !uri->opaque || !uri->path)
70     return gpg_error (GPG_ERR_INV_ARG);
71
72   name = xtrystrdup (uri->path);
73   if (!name)
74     return gpg_error_from_syserror ();
75
76   server = strchr (name, '@');
77   if (!server)
78     {
79       err = gpg_error (GPG_ERR_INV_URI);
80       xfree (name);
81       return err;
82     }
83   *server++ = 0;
84
85   err = http_raw_connect (&http, server, 79,
86                           ((dirmngr_use_tor ()? HTTP_FLAG_FORCE_TOR : 0)
87                            | (opt.disable_ipv4? HTTP_FLAG_IGNORE_IPv4 : 0)),
88                           NULL);
89   if (err)
90     {
91       xfree (name);
92       return err;
93     }
94
95   fp = http_get_write_ptr (http);
96   if (!fp)
97     {
98       err = gpg_error (GPG_ERR_INTERNAL);
99       http_close (http, 0);
100       xfree (name);
101       return err;
102     }
103
104   if (es_fputs (name, fp) || es_fputs ("\r\n", fp) || es_fflush (fp))
105     {
106       err = gpg_error_from_syserror ();
107       http_close (http, 0);
108       xfree (name);
109       return err;
110     }
111   xfree (name);
112   es_fclose (fp);
113
114   fp = http_get_read_ptr (http);
115   if (!fp)
116     {
117       err = gpg_error (GPG_ERR_INTERNAL);
118       http_close (http, 0);
119       return err;
120     }
121
122   http_close (http, 1 /* Keep read ptr.  */);
123
124   *r_fp = fp;
125   return 0;
126 }