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 "../common/userids.h"
30 #include "ks-engine.h"
31
32 /* Print a help output for the schemata supported by this module. */
33 gpg_error_t
ks_finger_help(ctrl_t ctrl,parsed_uri_t uri)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
ks_finger_fetch(ctrl_t ctrl,parsed_uri_t uri,estream_t * r_fp)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 (ctrl, &http, server, 79,
86 ((dirmngr_use_tor ()? HTTP_FLAG_FORCE_TOR : 0)
87 | (opt.disable_ipv4? HTTP_FLAG_IGNORE_IPv4 : 0)
88 | (opt.disable_ipv6? HTTP_FLAG_IGNORE_IPv6 : 0)),
89 NULL, ctrl->timeout);
90 if (err)
91 {
92 xfree (name);
93 return err;
94 }
95
96 fp = http_get_write_ptr (http);
97 if (!fp)
98 {
99 err = gpg_error (GPG_ERR_INTERNAL);
100 http_close (http, 0);
101 xfree (name);
102 return err;
103 }
104
105 if (es_fputs (name, fp) || es_fputs ("\r\n", fp) || es_fflush (fp))
106 {
107 err = gpg_error_from_syserror ();
108 http_close (http, 0);
109 xfree (name);
110 return err;
111 }
112 xfree (name);
113 es_fclose (fp);
114
115 fp = http_get_read_ptr (http);
116 if (!fp)
117 {
118 err = gpg_error (GPG_ERR_INTERNAL);
119 http_close (http, 0);
120 return err;
121 }
122
123 http_close (http, 1 /* Keep read ptr. */);
124
125 *r_fp = fp;
126 return 0;
127 }
128