xref: /minix3/crypto/external/bsd/netpgp/dist/src/hkpd/hkpd.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /*-
2*ebfedea0SLionel Sambuc  * Copyright (c) 2009,2010 The NetBSD Foundation, Inc.
3*ebfedea0SLionel Sambuc  * All rights reserved.
4*ebfedea0SLionel Sambuc  *
5*ebfedea0SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
6*ebfedea0SLionel Sambuc  * by Alistair Crooks (agc@NetBSD.org)
7*ebfedea0SLionel Sambuc  *
8*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10*ebfedea0SLionel Sambuc  * are met:
11*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
12*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
13*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
14*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
15*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
16*ebfedea0SLionel Sambuc  *
17*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18*ebfedea0SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19*ebfedea0SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20*ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21*ebfedea0SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*ebfedea0SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*ebfedea0SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*ebfedea0SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*ebfedea0SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*ebfedea0SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
28*ebfedea0SLionel Sambuc  */
29*ebfedea0SLionel Sambuc 
30*ebfedea0SLionel Sambuc #include <sys/types.h>
31*ebfedea0SLionel Sambuc #include <sys/param.h>
32*ebfedea0SLionel Sambuc #include <sys/socket.h>
33*ebfedea0SLionel Sambuc #include <sys/stat.h>
34*ebfedea0SLionel Sambuc #include <sys/select.h>
35*ebfedea0SLionel Sambuc 
36*ebfedea0SLionel Sambuc #include <netinet/in.h>
37*ebfedea0SLionel Sambuc 
38*ebfedea0SLionel Sambuc #include <errno.h>
39*ebfedea0SLionel Sambuc #include <netdb.h>
40*ebfedea0SLionel Sambuc #include <netpgp.h>
41*ebfedea0SLionel Sambuc #include <regex.h>
42*ebfedea0SLionel Sambuc #include <stdio.h>
43*ebfedea0SLionel Sambuc #include <stdlib.h>
44*ebfedea0SLionel Sambuc #include <string.h>
45*ebfedea0SLionel Sambuc #include <unistd.h>
46*ebfedea0SLionel Sambuc 
47*ebfedea0SLionel Sambuc #include "hkpd.h"
48*ebfedea0SLionel Sambuc 
49*ebfedea0SLionel Sambuc /* make the string have %xx -> %c */
50*ebfedea0SLionel Sambuc static size_t
frompercent(char * in,size_t insize,char * out,size_t outsize)51*ebfedea0SLionel Sambuc frompercent(char *in, size_t insize, char *out, size_t outsize)
52*ebfedea0SLionel Sambuc {
53*ebfedea0SLionel Sambuc 	size_t	 outcc;
54*ebfedea0SLionel Sambuc 	char	*next;
55*ebfedea0SLionel Sambuc 	char	*pc;
56*ebfedea0SLionel Sambuc 
57*ebfedea0SLionel Sambuc 	outcc = 0;
58*ebfedea0SLionel Sambuc 	for (pc = in ; (next = strchr(pc, '%')) != NULL ; pc = next + 3) {
59*ebfedea0SLionel Sambuc 		(void) memcpy(&out[outcc], pc, (size_t)(next - pc));
60*ebfedea0SLionel Sambuc 		outcc += (size_t)(next - pc);
61*ebfedea0SLionel Sambuc 		out[outcc++] = (char)strtol(next + 1, NULL, 16);
62*ebfedea0SLionel Sambuc 	}
63*ebfedea0SLionel Sambuc 	(void) memcpy(&out[outcc], pc, insize - (int)(pc - in));
64*ebfedea0SLionel Sambuc 	outcc += insize - (int)(pc - in);
65*ebfedea0SLionel Sambuc 	out[outcc] = 0x0;
66*ebfedea0SLionel Sambuc 	return outcc;
67*ebfedea0SLionel Sambuc }
68*ebfedea0SLionel Sambuc 
69*ebfedea0SLionel Sambuc #define HKP_HTTP_LEVEL	"HTTP/1.0"
70*ebfedea0SLionel Sambuc #define HKP_NAME	"hkpd"
71*ebfedea0SLionel Sambuc #define HKP_MIME_GET	"application/pgp-keys"
72*ebfedea0SLionel Sambuc #define HKP_MIME_INDEX	"text/plain"
73*ebfedea0SLionel Sambuc #define HKP_MACHREAD	"info:1:1\r\n"
74*ebfedea0SLionel Sambuc 
75*ebfedea0SLionel Sambuc #define HKP_SUCCESS	200
76*ebfedea0SLionel Sambuc #define HKP_NOT_FOUND	404
77*ebfedea0SLionel Sambuc 
78*ebfedea0SLionel Sambuc /* make into html */
79*ebfedea0SLionel Sambuc static int
htmlify(char * buf,size_t size,const int code,const int get,const char * title,const char * out,const char * body)80*ebfedea0SLionel Sambuc htmlify(char *buf, size_t size, const int code, const int get, const char *title, const char *out, const char *body)
81*ebfedea0SLionel Sambuc {
82*ebfedea0SLionel Sambuc 	return snprintf(buf, size,
83*ebfedea0SLionel Sambuc 		"%s %d %s\r\n"
84*ebfedea0SLionel Sambuc 		"Server: %s/%d\r\n"
85*ebfedea0SLionel Sambuc 		"Content-type: %s\r\n"
86*ebfedea0SLionel Sambuc 		"\r\n"
87*ebfedea0SLionel Sambuc 		"%s"
88*ebfedea0SLionel Sambuc 		"%s",
89*ebfedea0SLionel Sambuc 		HKP_HTTP_LEVEL, code, (code == HKP_SUCCESS) ? "OK" : "not found",
90*ebfedea0SLionel Sambuc 		HKP_NAME, HKPD_VERSION,
91*ebfedea0SLionel Sambuc 		(get) ? HKP_MIME_GET : HKP_MIME_INDEX,
92*ebfedea0SLionel Sambuc 		(get || strcmp(out, "mr") != 0) ? "" : HKP_MACHREAD,
93*ebfedea0SLionel Sambuc 		body);
94*ebfedea0SLionel Sambuc }
95*ebfedea0SLionel Sambuc 
96*ebfedea0SLionel Sambuc /* send the response now */
97*ebfedea0SLionel Sambuc static int
response(int sock,const int code,const char * search,const int get,char * buf,int cc,const char * out)98*ebfedea0SLionel Sambuc response(int sock, const int code, const char *search, const int get, char *buf, int cc, const char *out)
99*ebfedea0SLionel Sambuc {
100*ebfedea0SLionel Sambuc 	char	outbuf[1024 * 512];
101*ebfedea0SLionel Sambuc 	char	item[BUFSIZ];
102*ebfedea0SLionel Sambuc 	int	tot;
103*ebfedea0SLionel Sambuc 	int	wc;
104*ebfedea0SLionel Sambuc 	int	n;
105*ebfedea0SLionel Sambuc 
106*ebfedea0SLionel Sambuc 	if (buf == NULL) {
107*ebfedea0SLionel Sambuc 		(void) snprintf(item, sizeof(item),
108*ebfedea0SLionel Sambuc 			"Error handling request: No keys found for '%s'\r\n", search);
109*ebfedea0SLionel Sambuc 		n = htmlify(outbuf, sizeof(outbuf), code, get,
110*ebfedea0SLionel Sambuc 			"Error handling request\r\n",
111*ebfedea0SLionel Sambuc 			out,
112*ebfedea0SLionel Sambuc 			item);
113*ebfedea0SLionel Sambuc 	} else {
114*ebfedea0SLionel Sambuc 		(void) snprintf(item, sizeof(item), "Search results for '%s'", search);
115*ebfedea0SLionel Sambuc 		n = htmlify(outbuf, sizeof(outbuf), code, get,
116*ebfedea0SLionel Sambuc 			item,
117*ebfedea0SLionel Sambuc 			out,
118*ebfedea0SLionel Sambuc 			buf);
119*ebfedea0SLionel Sambuc 	}
120*ebfedea0SLionel Sambuc 	for (tot = 0 ; (wc = write(sock, &outbuf[tot], n - tot)) > 0 && tot < n ; tot += wc) {
121*ebfedea0SLionel Sambuc 	}
122*ebfedea0SLionel Sambuc 	return 1;
123*ebfedea0SLionel Sambuc }
124*ebfedea0SLionel Sambuc 
125*ebfedea0SLionel Sambuc /* get a socket (we'll bind it later) */
126*ebfedea0SLionel Sambuc static int
hkpd_sock_get(const int fam)127*ebfedea0SLionel Sambuc hkpd_sock_get(const int fam)
128*ebfedea0SLionel Sambuc {
129*ebfedea0SLionel Sambuc 	int	sock;
130*ebfedea0SLionel Sambuc 	int	on = 1;
131*ebfedea0SLionel Sambuc 
132*ebfedea0SLionel Sambuc 	sock = socket((fam == 4) ? AF_INET : AF_INET6, SOCK_STREAM, 0);
133*ebfedea0SLionel Sambuc 	if (sock < 0) {
134*ebfedea0SLionel Sambuc 		(void) fprintf(stderr,"hkpd_sock_get: can't get a socket\n");
135*ebfedea0SLionel Sambuc 		return -1;
136*ebfedea0SLionel Sambuc         }
137*ebfedea0SLionel Sambuc 	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
138*ebfedea0SLionel Sambuc 			(void *)&on, sizeof(on)) == -1) {
139*ebfedea0SLionel Sambuc 		(void) fprintf(stderr,
140*ebfedea0SLionel Sambuc 			"hkpd_sock_get: can't set SO_REUSEADDR\n");
141*ebfedea0SLionel Sambuc 		return -1;
142*ebfedea0SLionel Sambuc 	}
143*ebfedea0SLionel Sambuc 	if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
144*ebfedea0SLionel Sambuc 			(void *)&on, sizeof(on)) == -1) {
145*ebfedea0SLionel Sambuc 		(void) fprintf(stderr,
146*ebfedea0SLionel Sambuc 			"hkpd_sock_get: can't set SO_KEEPALIVE\n");
147*ebfedea0SLionel Sambuc 		return -1;
148*ebfedea0SLionel Sambuc 	}
149*ebfedea0SLionel Sambuc 	return sock;
150*ebfedea0SLionel Sambuc }
151*ebfedea0SLionel Sambuc 
152*ebfedea0SLionel Sambuc /**************************************************************************/
153*ebfedea0SLionel Sambuc 
154*ebfedea0SLionel Sambuc /* get a socket and bind it to the server */
155*ebfedea0SLionel Sambuc int
hkpd_sock_bind(const char * hostname,const int port,const int fam)156*ebfedea0SLionel Sambuc hkpd_sock_bind(const char *hostname, const int port, const int fam)
157*ebfedea0SLionel Sambuc {
158*ebfedea0SLionel Sambuc         struct addrinfo  hints;
159*ebfedea0SLionel Sambuc         struct addrinfo *res;
160*ebfedea0SLionel Sambuc         char             portstr[32];
161*ebfedea0SLionel Sambuc 	int		 sock;
162*ebfedea0SLionel Sambuc         int              rc = 0;
163*ebfedea0SLionel Sambuc 
164*ebfedea0SLionel Sambuc         (void) memset(&hints, 0, sizeof(hints));
165*ebfedea0SLionel Sambuc         hints.ai_family = (fam == 4) ? PF_INET : PF_INET6;
166*ebfedea0SLionel Sambuc         hints.ai_socktype = SOCK_STREAM;
167*ebfedea0SLionel Sambuc         (void) snprintf(portstr, sizeof(portstr), "%d", port);
168*ebfedea0SLionel Sambuc         /* Attempt connection */
169*ebfedea0SLionel Sambuc #ifdef AI_NUMERICSERV
170*ebfedea0SLionel Sambuc         hints.ai_flags = AI_NUMERICSERV;
171*ebfedea0SLionel Sambuc #endif
172*ebfedea0SLionel Sambuc         if ((rc = getaddrinfo(hostname, portstr, &hints, &res)) != 0) {
173*ebfedea0SLionel Sambuc                 hints.ai_flags = 0;
174*ebfedea0SLionel Sambuc                 if ((rc = getaddrinfo(hostname, "hkp", &hints, &res)) != 0) {
175*ebfedea0SLionel Sambuc                         (void) fprintf(stderr, "getaddrinfo: %s",
176*ebfedea0SLionel Sambuc 					gai_strerror(rc));
177*ebfedea0SLionel Sambuc                         return -1;
178*ebfedea0SLionel Sambuc                 }
179*ebfedea0SLionel Sambuc         }
180*ebfedea0SLionel Sambuc 	if ((sock = hkpd_sock_get(fam)) < 0) {
181*ebfedea0SLionel Sambuc                 (void) fprintf(stderr, "hkpd_sock_get failed %d\n", errno);
182*ebfedea0SLionel Sambuc                 freeaddrinfo(res);
183*ebfedea0SLionel Sambuc                 return -1;
184*ebfedea0SLionel Sambuc 	}
185*ebfedea0SLionel Sambuc         if ((rc = bind(sock, res->ai_addr, res->ai_addrlen)) < 0) {
186*ebfedea0SLionel Sambuc                 (void) fprintf(stderr, "bind failed %d\n", errno);
187*ebfedea0SLionel Sambuc                 freeaddrinfo(res);
188*ebfedea0SLionel Sambuc                 return -1;
189*ebfedea0SLionel Sambuc         }
190*ebfedea0SLionel Sambuc         freeaddrinfo(res);
191*ebfedea0SLionel Sambuc         if (rc < 0) {
192*ebfedea0SLionel Sambuc                 (void) fprintf(stderr, "bind() to %s:%d failed (rc %d)\n",
193*ebfedea0SLionel Sambuc 				hostname, port, rc);
194*ebfedea0SLionel Sambuc         }
195*ebfedea0SLionel Sambuc         return sock;
196*ebfedea0SLionel Sambuc }
197*ebfedea0SLionel Sambuc 
198*ebfedea0SLionel Sambuc /* netpgp key daemon - does not return */
199*ebfedea0SLionel Sambuc int
hkpd(netpgp_t * netpgp,int sock4,int sock6)200*ebfedea0SLionel Sambuc hkpd(netpgp_t *netpgp, int sock4, int sock6)
201*ebfedea0SLionel Sambuc {
202*ebfedea0SLionel Sambuc 	struct sockaddr_in	from;
203*ebfedea0SLionel Sambuc 	regmatch_t		searchmatches[10];
204*ebfedea0SLionel Sambuc 	regmatch_t		opmatches[10];
205*ebfedea0SLionel Sambuc 	regmatch_t		fmtmatch[3];
206*ebfedea0SLionel Sambuc 	socklen_t		fromlen;
207*ebfedea0SLionel Sambuc 	regex_t			searchterm;
208*ebfedea0SLionel Sambuc 	regex_t			fmtterm;
209*ebfedea0SLionel Sambuc 	regex_t			opterm;
210*ebfedea0SLionel Sambuc 	regex_t			get;
211*ebfedea0SLionel Sambuc 	fd_set			sockets;
212*ebfedea0SLionel Sambuc 	char			search[BUFSIZ];
213*ebfedea0SLionel Sambuc 	char			buf[BUFSIZ];
214*ebfedea0SLionel Sambuc 	char			*cp;
215*ebfedea0SLionel Sambuc 	char			fmt[10];
216*ebfedea0SLionel Sambuc 	int			newsock;
217*ebfedea0SLionel Sambuc 	int			sock;
218*ebfedea0SLionel Sambuc 	int			code;
219*ebfedea0SLionel Sambuc 	int			ok;
220*ebfedea0SLionel Sambuc 	int			cc;
221*ebfedea0SLionel Sambuc 	int			n;
222*ebfedea0SLionel Sambuc 
223*ebfedea0SLionel Sambuc /* GET /pks/lookup?search=agc%40netbsd.org&op=index&options=mr HTTP/1.1\n */
224*ebfedea0SLionel Sambuc #define HTTPGET		"GET /pks/lookup\\?"
225*ebfedea0SLionel Sambuc #define OPTERM		"op=([a-zA-Z]+)"
226*ebfedea0SLionel Sambuc #define SEARCHTERM	"search=([^ \t&]+)"
227*ebfedea0SLionel Sambuc #define FMT		"options=(mr|json)"
228*ebfedea0SLionel Sambuc 
229*ebfedea0SLionel Sambuc 	(void) regcomp(&get, HTTPGET, REG_EXTENDED);
230*ebfedea0SLionel Sambuc 	(void) regcomp(&opterm, OPTERM, REG_EXTENDED);
231*ebfedea0SLionel Sambuc 	(void) regcomp(&searchterm, SEARCHTERM, REG_EXTENDED);
232*ebfedea0SLionel Sambuc 	(void) regcomp(&fmtterm, FMT, REG_EXTENDED);
233*ebfedea0SLionel Sambuc 	if (sock4 >= 0) {
234*ebfedea0SLionel Sambuc 		listen(sock4, 32);
235*ebfedea0SLionel Sambuc 	}
236*ebfedea0SLionel Sambuc 	if (sock6 >= 0) {
237*ebfedea0SLionel Sambuc 		listen(sock6, 32);
238*ebfedea0SLionel Sambuc 	}
239*ebfedea0SLionel Sambuc 	for (;;) {
240*ebfedea0SLionel Sambuc 		/* find out which socket we have data on */
241*ebfedea0SLionel Sambuc 		FD_ZERO(&sockets);
242*ebfedea0SLionel Sambuc 		if (sock4 >= 0) {
243*ebfedea0SLionel Sambuc 			FD_SET(sock4, &sockets);
244*ebfedea0SLionel Sambuc 		}
245*ebfedea0SLionel Sambuc 		if (sock6 >= 0) {
246*ebfedea0SLionel Sambuc 			FD_SET(sock6, &sockets);
247*ebfedea0SLionel Sambuc 		}
248*ebfedea0SLionel Sambuc 		if (select(32, &sockets, NULL, NULL, NULL) < 0) {
249*ebfedea0SLionel Sambuc 			(void) fprintf(stderr, "bad select call\n");
250*ebfedea0SLionel Sambuc 			continue;
251*ebfedea0SLionel Sambuc 		}
252*ebfedea0SLionel Sambuc 		sock = (sock4 >= 0 && FD_ISSET(sock4, &sockets)) ? sock4 : sock6;
253*ebfedea0SLionel Sambuc 		/* read data from socket */
254*ebfedea0SLionel Sambuc 		fromlen = sizeof(from);
255*ebfedea0SLionel Sambuc 		newsock = accept(sock, (struct sockaddr *) &from, &fromlen);
256*ebfedea0SLionel Sambuc 		cc = read(newsock, buf, sizeof(buf));
257*ebfedea0SLionel Sambuc 		/* parse the request */
258*ebfedea0SLionel Sambuc 		ok = 1;
259*ebfedea0SLionel Sambuc 		if (regexec(&get, buf, 10, opmatches, 0) != 0) {
260*ebfedea0SLionel Sambuc 			(void) fprintf(stderr, "not a valid get request\n");
261*ebfedea0SLionel Sambuc 			ok = 0;
262*ebfedea0SLionel Sambuc 		}
263*ebfedea0SLionel Sambuc 		if (ok && regexec(&opterm, buf, 10, opmatches, 0) != 0) {
264*ebfedea0SLionel Sambuc 			(void) fprintf(stderr, "no operation in request\n");
265*ebfedea0SLionel Sambuc 			ok = 0;
266*ebfedea0SLionel Sambuc 		}
267*ebfedea0SLionel Sambuc 		if (ok && regexec(&fmtterm, buf, 3, fmtmatch, 0) == 0) {
268*ebfedea0SLionel Sambuc 			(void) snprintf(fmt, sizeof(fmt), "%.*s",
269*ebfedea0SLionel Sambuc 				(int)(fmtmatch[1].rm_eo - fmtmatch[1].rm_so),
270*ebfedea0SLionel Sambuc 				&buf[(int)fmtmatch[1].rm_so]);
271*ebfedea0SLionel Sambuc 		} else {
272*ebfedea0SLionel Sambuc 			fmt[0] = 0x0;
273*ebfedea0SLionel Sambuc 		}
274*ebfedea0SLionel Sambuc 		if (ok && regexec(&searchterm, buf, 10, searchmatches, 0) != 0) {
275*ebfedea0SLionel Sambuc 			(void) fprintf(stderr, "no search term in request\n");
276*ebfedea0SLionel Sambuc 			ok = 0;
277*ebfedea0SLionel Sambuc 		}
278*ebfedea0SLionel Sambuc 		if (!ok) {
279*ebfedea0SLionel Sambuc 			(void) close(newsock);
280*ebfedea0SLionel Sambuc 			continue;
281*ebfedea0SLionel Sambuc 		}
282*ebfedea0SLionel Sambuc 		/* convert from %2f to / etc */
283*ebfedea0SLionel Sambuc 		n = frompercent(&buf[searchmatches[1].rm_so],
284*ebfedea0SLionel Sambuc 			(int)(searchmatches[1].rm_eo - searchmatches[1].rm_so),
285*ebfedea0SLionel Sambuc 			search,
286*ebfedea0SLionel Sambuc 			sizeof(search));
287*ebfedea0SLionel Sambuc 		code = HKP_NOT_FOUND;
288*ebfedea0SLionel Sambuc 		cc = 0;
289*ebfedea0SLionel Sambuc 		if (strncmp(&buf[opmatches[1].rm_so], "vindex", 6) == 0) {
290*ebfedea0SLionel Sambuc 			cc = 0;
291*ebfedea0SLionel Sambuc 			netpgp_setvar(netpgp, "subkey sigs", "yes");
292*ebfedea0SLionel Sambuc 			if (strcmp(fmt, "json") == 0) {
293*ebfedea0SLionel Sambuc 				if (netpgp_match_keys_json(netpgp, &cp, search, "human", 1)) {
294*ebfedea0SLionel Sambuc 					cc = strlen(cp);
295*ebfedea0SLionel Sambuc 					code = HKP_SUCCESS;
296*ebfedea0SLionel Sambuc 				}
297*ebfedea0SLionel Sambuc 			} else if ((cp = netpgp_get_key(netpgp, search, fmt)) != NULL) {
298*ebfedea0SLionel Sambuc 				cc = strlen(cp);
299*ebfedea0SLionel Sambuc 				code = HKP_SUCCESS;
300*ebfedea0SLionel Sambuc 			}
301*ebfedea0SLionel Sambuc 			response(newsock, code, search, 0, cp, cc, fmt);
302*ebfedea0SLionel Sambuc 			netpgp_unsetvar(netpgp, "subkey sigs");
303*ebfedea0SLionel Sambuc 		} else if (strncmp(&buf[opmatches[1].rm_so], "index", 5) == 0) {
304*ebfedea0SLionel Sambuc 			cc = 0;
305*ebfedea0SLionel Sambuc 			netpgp_unsetvar(netpgp, "subkey sigs");
306*ebfedea0SLionel Sambuc 			if (strcmp(fmt, "json") == 0) {
307*ebfedea0SLionel Sambuc 				if (netpgp_match_keys_json(netpgp, &cp, search, "human", 0)) {
308*ebfedea0SLionel Sambuc 					cc = strlen(cp);
309*ebfedea0SLionel Sambuc 					code = HKP_SUCCESS;
310*ebfedea0SLionel Sambuc 				}
311*ebfedea0SLionel Sambuc 			} else if ((cp = netpgp_get_key(netpgp, search, fmt)) != NULL) {
312*ebfedea0SLionel Sambuc 				cc = strlen(cp);
313*ebfedea0SLionel Sambuc 				code = HKP_SUCCESS;
314*ebfedea0SLionel Sambuc 			}
315*ebfedea0SLionel Sambuc 			response(newsock, code, search, 0, cp, cc, fmt);
316*ebfedea0SLionel Sambuc 		} else if (strncmp(&buf[opmatches[1].rm_so], "get", 3) == 0) {
317*ebfedea0SLionel Sambuc 			if ((cp = netpgp_export_key(netpgp, search)) != NULL) {
318*ebfedea0SLionel Sambuc 				cc = strlen(cp);
319*ebfedea0SLionel Sambuc 				code = HKP_SUCCESS;
320*ebfedea0SLionel Sambuc 			}
321*ebfedea0SLionel Sambuc 			response(newsock, code, search, 1, cp, cc, fmt);
322*ebfedea0SLionel Sambuc 		}
323*ebfedea0SLionel Sambuc 		free(cp);
324*ebfedea0SLionel Sambuc 		(void) close(newsock);
325*ebfedea0SLionel Sambuc 	}
326*ebfedea0SLionel Sambuc }
327