~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to g10/ks-proto.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ks-proto.c  keyserver protocol handling
2
 
 * Copyright (C) 1998 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 2 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, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19
 
 */
20
 
 
21
 
/****************
22
 
 * The extended HKP protocol:
23
 
 *
24
 
 *  GET /pks/lookup[/<gnupg_user_id>][?[op=<cmd>][&armor=0][&search=<keywords>]]
25
 
 *
26
 
 * Default is: "armor=1", "op=get". "search" is only allowed if gnupg_user_id
27
 
 * is not present.  GET maybe replaced by HEAD in which case only some status
28
 
 * information is returned.
29
 
 *
30
 
 * Hmmm, I don't like it, the better solution is to use:
31
 
 *
32
 
 *  /pks/gnupg/get for binary lookups
33
 
 *  /pks/gnupg/upd to update a key
34
 
 *  /pks/gnupg/ins to insert a new key
35
 
 *
36
 
 * Optional a version string can be inserted as in:
37
 
 *
38
 
 *  /pks/gnupg/v1.0/get
39
 
 *
40
 
 * Returned HTTP options:
41
 
 *  X-Key-Hash: <rmd160 hash value of the keyblock>
42
 
 *  X-Key-MTime: <last modification time>
43
 
 *  X-Key-LID: <local_key_id_used_for_update_etc>
44
 
 * [fixme: is X-.... allowed?]
45
 
 *
46
 
 */
47
 
 
48
 
#include <config.h>
49
 
#include <stdio.h>
50
 
#include <stdlib.h>
51
 
#include <string.h>
52
 
#include <errno.h>
53
 
#include <ctype.h>
54
 
#include "util.h"
55
 
#include "ks-proto.h"
56
 
 
57
 
 
58
 
static int
59
 
do_read( int fd, char *buffer, size_t bufsize, int *ret_nread )
60
 
{
61
 
    int n;
62
 
    fd_set rfds;
63
 
    struct timeval tv;
64
 
    int rc;
65
 
 
66
 
    *ret_nread = 0;
67
 
    do {
68
 
        FD_ZERO(&rfds);
69
 
        FD_SET(fd, &rfds);
70
 
        tv.tv_sec = 1;
71
 
        tv.tv_usec = 0;
72
 
        if( !(rc=select(fd+1, &rfds, NULL, NULL, &tv)) )
73
 
            return 0; /* timeout */
74
 
        if( rc == -1 ) {
75
 
            log_error("select() error: %s\n", strerror(errno));
76
 
            return -1;
77
 
        }
78
 
 
79
 
        do {
80
 
            n = read(fd, buffer, bufsize );
81
 
            if( n >= 0 && n > bufsize )
82
 
                log_bug("bogus read from fd %d (n=%d)\n", fd, n );
83
 
        } while( n == -1 && errno == EINTR );
84
 
        if( n == -1 ) {
85
 
            log_error("read error on fd %d: %s\n", fd, strerror(errno) );
86
 
            return -1;
87
 
        }
88
 
    } while( !n );
89
 
    *ret_nread = n;
90
 
    return 0;
91
 
}
92
 
 
93
 
 
94
 
int
95
 
ks_get_request( int fd, KS_TRANS *req )
96
 
{
97
 
    char *p, *p2, buf[500];
98
 
    int nread, n;
99
 
    int state = 0;
100
 
 
101
 
    req->err = 0;
102
 
    req->data = NULL;
103
 
    while( !do_read( fd, buf, DIM(buf)-1, &nread ) {
104
 
        p = buf;
105
 
        if( !state ) {
106
 
            /* replace the trailing LF with a 0 */
107
 
            for(p2=p,n=0; n < nread && *p2 != '\n'; p2++ )
108
 
                ;
109
 
            if( *p2 != '\n' ) {
110
 
                req->err = KS_ERR_REQ_TOO_LONG;
111
 
                break;
112
 
            }
113
 
            *p2++ = 0;
114
 
            n++;
115
 
 
116
 
            /* now look at the request.  Note that the isspace() will work
117
 
             * because there is still a CR before the 0 */
118
 
            if(      (p[0] == 'G' || p[0] == 'g')
119
 
                  && (p[1] == 'E' || p[1] == 'e')
120
 
                  && (p[2] == 'T' || p[2] == 't') && isspace( p[3] ) ) {
121
 
                req->cmd = KS_REQ_GET;
122
 
                p += 4;
123
 
            }
124
 
            else if( (p[0] == 'H' || p[0] == 'h')
125
 
                  && (p[1] == 'E' || p[1] == 'e')
126
 
                  && (p[2] == 'A' || p[2] == 'a')
127
 
                  && (p[3] == 'D' || p[3] == 'd') && isspace( p[4] ) ) {
128
 
                req->cmd = KS_REQ_HEAD;
129
 
                p += 5;
130
 
            }
131
 
            else if( (p[0] == 'H' || p[0] == 'h')
132
 
                  && (p[1] == 'E' || p[1] == 'e')
133
 
                  && (p[2] == 'L' || p[2] == 'l')
134
 
                  && (p[3] == 'P' || p[3] == 'p') && isspace( p[4] ) ) {
135
 
                req->cmd = KS_REQ_HELP;
136
 
                p += 5;
137
 
            }
138
 
            else
139
 
                req->cmd = KS_REQ_UNKNOWN;
140
 
            /* skip spaces, store args and remaining data */
141
 
            while( *p == ' ' || *p == '\t' )
142
 
                p++;
143
 
            /* fixme: remove trailing blanks from args */
144
 
            req->args = p;
145
 
            p = p2; /* p now points to the remaining n bytes in the buffer */
146
 
            state = 1;
147
 
        }
148
 
        if( state == 1 ) {
149
 
            /* read the option lines */
150
 
        }
151
 
 
152
 
    }
153
 
}
154
 
 
155