1
/* gpgkeys_finger.c - fetch a key via finger
2
* Copyright (C) 2004 Free Software Foundation, Inc.
4
* This file is part of GnuPG.
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 2 of the License, or
9
* (at your option) any later version.
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.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
35
#include <sys/types.h>
36
#include <sys/socket.h>
39
#include <netinet/in.h>
40
#include <arpa/inet.h>
44
#define INCLUDED_BY_MAIN_MODULE 1
46
#include "keyserver.h"
50
#define sock_close(a) closesocket(a)
52
#define sock_close(a) close(a)
62
static char path[1024];
63
static FILE *input, *output, *console;
65
#define BEGIN "-----BEGIN PGP PUBLIC KEY BLOCK-----"
66
#define END "-----END PGP PUBLIC KEY BLOCK-----"
78
static int initialized;
79
static WSADATA wsdata;
84
if (WSAStartup (0x0101, &wsdata) )
86
fprintf (console, "error initializing socket library: ec=%d\n",
87
(int)WSAGetLastError () );
90
if (wsdata.wVersion < 0x0001)
92
fprintf (console, "socket library version is %x.%x - but 1.1 needed\n",
93
LOBYTE(wsdata.wVersion), HIBYTE(wsdata.wVersion));
97
atexit (deinit_sockets);
103
/* Connect to SERVER at PORT and return a file descriptor or -1 on
106
connect_server (const char *server, unsigned short port)
112
struct sockaddr_in addr;
117
memset (&addr, 0, sizeof addr);
118
addr.sin_family = AF_INET;
119
addr.sin_port = htons (port);
121
/* Win32 gethostbyname doesn't handle IP addresses internally, so we
122
try inet_addr first on that platform only. */
123
if ((l = inet_addr (server)) != SOCKET_ERROR)
124
memcpy (&addr.sin_addr, &l, sizeof l);
125
else if ((hp = gethostbyname (server)))
127
if (hp->h_addrtype != AF_INET)
129
fprintf (console, "gpgkeys: unknown address family for `%s'\n",
133
if (hp->h_length != 4)
135
fprintf (console, "gpgkeys: illegal address length for `%s'\n",
139
memcpy (&addr.sin_addr, hp->h_addr, hp->h_length);
143
fprintf (console, "gpgkeys: host `%s' not found: ec=%d\n",
144
server, (int)WSAGetLastError ());
148
sock = socket (AF_INET, SOCK_STREAM, 0);
149
if (sock == INVALID_SOCKET)
151
fprintf (console, "gpgkeys: error creating socket: ec=%d\n",
152
(int)WSAGetLastError ());
156
if (connect (sock, (struct sockaddr *)&addr, sizeof addr))
158
fprintf (console, "gpgkeys: error connecting `%s': ec=%d\n",
159
server, (int)WSAGetLastError ());
166
struct sockaddr_in addr;
167
struct hostent *host;
169
addr.sin_family = AF_INET;
170
addr.sin_port = htons (port);
171
host = gethostbyname ((char*)server);
174
fprintf (console, "gpgkeys: host `%s' not found: %s\n",
175
server, strerror (errno));
179
addr.sin_addr = *(struct in_addr*)host->h_addr;
181
sock = socket (AF_INET, SOCK_STREAM, 0);
184
fprintf (console, "gpgkeys: error creating socket: %s\n",
189
if (connect (sock, (struct sockaddr *)&addr, sizeof addr) == -1)
191
fprintf (console, "gpgkeys: error connecting `%s': %s\n",
192
server, strerror (errno));
202
write_server (int sock, const char *data, size_t length)
212
nwritten = send (sock, data, nleft, 0);
213
if ( nwritten == SOCKET_ERROR )
215
fprintf (console, "gpgkeys: write failed: ec=%d\n",
216
(int)WSAGetLastError ());
220
nwritten = write (sock, data, nleft);
231
select(0, NULL, NULL, NULL, &tv);
234
fprintf (console, "gpgkeys: write failed: %s\n", strerror(errno));
246
/* Send the finger REQUEST to the server. Returns 0 and a file descriptor
247
in R_SOCK if the request was sucessful. */
249
send_request (const char *request, int *r_sock)
256
name = strdup (request);
259
fprintf(console,"gpgkeys: out of memory\n");
260
return KEYSERVER_NO_MEMORY;
263
server = strchr (name, '@');
266
fprintf (console, "gpgkeys: no name included in request\n");
268
return KEYSERVER_GENERAL_ERROR;
272
sock = connect_server (server, 79);
276
return KEYSERVER_UNREACHABLE;
279
if (write_server (sock, name, strlen (name))
280
|| write_server (sock, "\r\n", 2))
284
return KEYSERVER_GENERAL_ERROR;
294
get_key (char *getkey)
299
unsigned int maxlen, buflen, gotit=0;
302
if (strncmp (getkey,"0x",2)==0)
305
/* Frankly we don't know what keys the server will return; we
306
indicated the requested key anyway. */
307
fprintf(output,"KEY 0x%s BEGIN\n",getkey);
309
rc = send_request (path, &sock);
312
fprintf(output,"KEY 0x%s FAILED %d\n",getkey, rc);
317
/* Hmmm, we use iobuf here only to cope with Windows socket
318
peculiarities (we can't used fdopen). */
319
fp_read = iobuf_sockopen (sock , "r");
322
fprintf(output,"KEY 0x%s FAILED %d\n",getkey, KEYSERVER_INTERNAL_ERROR);
327
while ( iobuf_read_line ( fp_read, &line, &buflen, &maxlen))
333
fputs (line, output);
334
if (!strncmp(line,END,strlen(END)))
337
else if(!strncmp(line,BEGIN,strlen(BEGIN)))
345
fprintf (output,"KEY 0x%s END\n", getkey);
348
fprintf (console,"gpgkeys: key %s not found on keyserver\n",getkey);
349
fprintf (output,"KEY 0x%s FAILED %d\n",
350
getkey,KEYSERVER_KEY_NOT_FOUND);
354
iobuf_close (fp_read);
363
fprintf (fp,"-h\thelp\n");
364
fprintf (fp,"-V\tversion\n");
365
fprintf (fp,"-o\toutput to this file\n");
369
main(int argc,char *argv[])
371
int arg,action=-1,ret=KEYSERVER_INTERNAL_ERROR;
374
unsigned int timeout=DEFAULT_KEYSERVER_TIMEOUT;
378
/* Kludge to implement standard GNU options. */
379
if (argc > 1 && !strcmp (argv[1], "--version"))
381
fputs ("gpgkeys_ldap (GnuPG) " VERSION"\n", stdout);
384
else if (argc > 1 && !strcmp (argv[1], "--help"))
390
while((arg=getopt(argc,argv,"hVo:"))!=-1)
399
fprintf(stdout,"%d\n%s\n",KEYSERVER_PROTO_VERSION,VERSION);
403
output=fopen(optarg,"w");
406
fprintf(console,"gpgkeys: Cannot open output file `%s': %s\n",
407
optarg,strerror(errno));
408
return KEYSERVER_INTERNAL_ERROR;
416
input=fopen(argv[optind],"r");
419
fprintf(console,"gpgkeys: Cannot open input file `%s': %s\n",
420
argv[optind],strerror(errno));
421
return KEYSERVER_INTERNAL_ERROR;
431
/* Get the command and info block */
433
while(fgets(line,MAX_LINE,input)!=NULL)
443
if(sscanf(line,"%c",&hash)==1 && hash=='#')
446
if(sscanf(line,"COMMAND %6s\n",commandstr)==1)
450
if(strcasecmp(commandstr,"get")==0)
456
if(strncmp(line,"HOST ",5)==0)
458
fprintf(console,"gpgkeys: finger://relay/user syntax is not"
459
" supported. Use finger:user instead.\n");
460
ret=KEYSERVER_NOT_SUPPORTED;
464
if(sscanf(line,"OPAQUE %1023s\n",path)==1)
470
if(sscanf(line,"VERSION %d\n",&version)==1)
472
if(version!=KEYSERVER_PROTO_VERSION)
474
ret=KEYSERVER_VERSION_ERROR;
481
if(sscanf(line,"OPTION %255s\n",optionstr)==1)
484
char *start=&optionstr[0];
488
if(strncasecmp(optionstr,"no-",3)==0)
494
if(strcasecmp(start,"verbose")==0)
501
else if(strncasecmp(start,"timeout",7)==0)
505
else if(start[7]=='=')
506
timeout=atoi(&start[8]);
507
else if(start[7]=='\0')
508
timeout=DEFAULT_KEYSERVER_TIMEOUT;
515
if(timeout && register_timeout()==-1)
517
fprintf(console,"gpgkeys: unable to register timeout handler\n");
518
return KEYSERVER_INTERNAL_ERROR;
521
/* If it's a GET or a SEARCH, the next thing to come in is the
522
keyids. If it's a SEND, then there are no keyids. */
526
/* Eat the rest of the file */
529
if(fgets(line,MAX_LINE,input)==NULL)
533
if(line[0]=='\n' || line[0]=='\0')
541
fprintf(console,"gpgkeys: out of memory while "
542
"building key list\n");
543
ret=KEYSERVER_NO_MEMORY;
547
/* Trim the trailing \n */
548
thekey[strlen(line)-1]='\0';
556
"gpgkeys: this keyserver type only supports key retrieval\n");
560
if(!thekey || !*path)
562
fprintf(console,"gpgkeys: invalid keyserver instructions\n");
566
/* Send the response */
568
fprintf(output,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
569
fprintf(output,"PROGRAM %s\n\n",VERSION);
574
fprintf(console,"Path:\t\t%s\n",path);
575
fprintf(console,"Command:\tGET\n");
578
set_timeout(timeout);
580
ret = get_key(thekey);