chiark / gitweb /
asshelp.c: add a lot of debug logging
[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                           (opt.use_tor? HTTP_FLAG_FORCE_TOR : 0), NULL);
87   if (err)
88     {
89       xfree (name);
90       return err;
91     }
92
93   fp = http_get_write_ptr (http);
94   if (!fp)
95     {
96       err = gpg_error (GPG_ERR_INTERNAL);
97       http_close (http, 0);
98       xfree (name);
99       return err;
100     }
101
102   if (es_fputs (name, fp) || es_fputs ("\r\n", fp) || es_fflush (fp))
103     {
104       err = gpg_error_from_syserror ();
105       http_close (http, 0);
106       xfree (name);
107       return err;
108     }
109   xfree (name);
110   es_fclose (fp);
111
112   fp = http_get_read_ptr (http);
113   if (!fp)
114     {
115       err = gpg_error (GPG_ERR_INTERNAL);
116       http_close (http, 0);
117       return err;
118     }
119
120   http_close (http, 1 /* Keep read ptr.  */);
121
122   *r_fp = fp;
123   return 0;
124 }