~ubuntu-branches/ubuntu/lucid/curl/lucid-201101212007

« back to all changes in this revision

Viewing changes to lib/security.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2008-02-08 11:20:41 UTC
  • mto: (3.1.1 lenny) (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 26.
  • Revision ID: james.westby@ubuntu.com-20080208112041-hed7sb5r6ghmjf8v
Tags: upstream-7.18.0
Import upstream version 7.18.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 *
10
10
 * Copyright (c) 1998, 1999 Kungliga Tekniska H�gskolan
11
11
 * (Royal Institute of Technology, Stockholm, Sweden).
 
12
 *
 
13
 * Copyright (C) 2001 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
 
14
 *
12
15
 * All rights reserved.
13
16
 *
14
17
 * Redistribution and use in source and binary forms, with or without
67
70
#define min(a, b)   ((a) < (b) ? (a) : (b))
68
71
 
69
72
static const struct {
70
 
    enum protection_level level;
71
 
    const char *name;
 
73
  enum protection_level level;
 
74
  const char *name;
72
75
} level_names[] = {
73
 
    { prot_clear, "clear" },
74
 
    { prot_safe, "safe" },
75
 
    { prot_confidential, "confidential" },
76
 
    { prot_private, "private" }
 
76
  { prot_clear, "clear" },
 
77
  { prot_safe, "safe" },
 
78
  { prot_confidential, "confidential" },
 
79
  { prot_private, "private" }
77
80
};
78
81
 
79
82
static enum protection_level
88
91
 
89
92
static const struct Curl_sec_client_mech * const mechs[] = {
90
93
#ifdef HAVE_GSSAPI
91
 
    &Curl_krb5_client_mech,
 
94
  &Curl_krb5_client_mech,
92
95
#endif
93
96
#ifdef HAVE_KRB4
94
 
    &Curl_krb4_client_mech,
 
97
  &Curl_krb4_client_mech,
95
98
#endif
96
 
    NULL
 
99
  NULL
97
100
};
98
101
 
99
102
int
116
119
  int b;
117
120
  while(len) {
118
121
    b = read(fd, p, len);
119
 
    if (b == 0)
 
122
    if(b == 0)
120
123
      return 0;
121
 
    else if (b < 0 && (errno == EINTR || errno == EAGAIN))
 
124
    else if(b < 0 && (errno == EINTR || errno == EAGAIN))
122
125
      continue;
123
 
    else if (b < 0)
 
126
    else if(b < 0)
124
127
      return -1;
125
128
    len -= b;
126
129
    p += b;
135
138
  int b;
136
139
  while(len) {
137
140
    b = write(fd, p, len);
138
 
    if (b < 0 && (errno == EINTR || errno == EAGAIN))
 
141
    if(b < 0 && (errno == EINTR || errno == EAGAIN))
139
142
      continue;
140
143
    else if(b < 0)
141
144
      return -1;
153
156
  int b;
154
157
 
155
158
  b = block_read(fd, &len, sizeof(len));
156
 
  if (b == 0)
 
159
  if(b == 0)
157
160
    return 0;
158
 
  else if (b < 0)
 
161
  else if(b < 0)
159
162
    return -1;
160
163
  len = ntohl(len);
161
164
  buf->data = realloc(buf->data, len);
162
165
  b = buf->data ? block_read(fd, buf->data, len) : -1;
163
 
  if (b == 0)
 
166
  if(b == 0)
164
167
    return 0;
165
 
  else if (b < 0)
 
168
  else if(b < 0)
166
169
    return -1;
167
170
  buf->size = (conn->mech->decode)(conn->app_data, buf->data, len,
168
171
                                   conn->data_prot, conn);
173
176
static size_t
174
177
buffer_read(struct krb4buffer *buf, void *data, size_t len)
175
178
{
176
 
    len = min(len, buf->size - buf->index);
177
 
    memcpy(data, (char*)buf->data + buf->index, len);
178
 
    buf->index += len;
179
 
    return len;
 
179
  len = min(len, buf->size - buf->index);
 
180
  memcpy(data, (char*)buf->data + buf->index, len);
 
181
  buf->index += len;
 
182
  return len;
180
183
}
181
184
 
182
185
static size_t
183
186
buffer_write(struct krb4buffer *buf, void *data, size_t len)
184
187
{
185
 
    if(buf->index + len > buf->size) {
186
 
        void *tmp;
187
 
        if(buf->data == NULL)
188
 
            tmp = malloc(1024);
189
 
        else
190
 
            tmp = realloc(buf->data, buf->index + len);
191
 
        if(tmp == NULL)
192
 
            return -1;
193
 
        buf->data = tmp;
194
 
        buf->size = buf->index + len;
195
 
    }
196
 
    memcpy((char*)buf->data + buf->index, data, len);
197
 
    buf->index += len;
198
 
    return len;
 
188
  if(buf->index + len > buf->size) {
 
189
    void *tmp;
 
190
    if(buf->data == NULL)
 
191
      tmp = malloc(1024);
 
192
    else
 
193
      tmp = realloc(buf->data, buf->index + len);
 
194
    if(tmp == NULL)
 
195
      return -1;
 
196
    buf->data = tmp;
 
197
    buf->size = buf->index + len;
 
198
  }
 
199
  memcpy((char*)buf->data + buf->index, data, len);
 
200
  buf->index += len;
 
201
  return len;
199
202
}
200
203
 
201
204
int
202
205
Curl_sec_read(struct connectdata *conn, int fd, void *buffer, int length)
203
206
{
204
 
    size_t len;
205
 
    int rx = 0;
206
 
 
207
 
    if(conn->sec_complete == 0 || conn->data_prot == 0)
208
 
      return read(fd, buffer, length);
209
 
 
210
 
    if(conn->in_buffer.eof_flag){
211
 
      conn->in_buffer.eof_flag = 0;
212
 
      return 0;
 
207
  size_t len;
 
208
  int rx = 0;
 
209
 
 
210
  if(conn->sec_complete == 0 || conn->data_prot == 0)
 
211
    return read(fd, buffer, length);
 
212
 
 
213
  if(conn->in_buffer.eof_flag){
 
214
    conn->in_buffer.eof_flag = 0;
 
215
    return 0;
 
216
  }
 
217
 
 
218
  len = buffer_read(&conn->in_buffer, buffer, length);
 
219
  length -= len;
 
220
  rx += len;
 
221
  buffer = (char*)buffer + len;
 
222
 
 
223
  while(length) {
 
224
    if(sec_get_data(conn, fd, &conn->in_buffer) < 0)
 
225
      return -1;
 
226
    if(conn->in_buffer.size == 0) {
 
227
      if(rx)
 
228
        conn->in_buffer.eof_flag = 1;
 
229
      return rx;
213
230
    }
214
 
 
215
231
    len = buffer_read(&conn->in_buffer, buffer, length);
216
232
    length -= len;
217
233
    rx += len;
218
234
    buffer = (char*)buffer + len;
219
 
 
220
 
    while(length) {
221
 
      if(sec_get_data(conn, fd, &conn->in_buffer) < 0)
222
 
        return -1;
223
 
      if(conn->in_buffer.size == 0) {
224
 
        if(rx)
225
 
          conn->in_buffer.eof_flag = 1;
226
 
        return rx;
227
 
      }
228
 
      len = buffer_read(&conn->in_buffer, buffer, length);
229
 
      length -= len;
230
 
      rx += len;
231
 
      buffer = (char*)buffer + len;
232
 
    }
233
 
    return rx;
 
235
  }
 
236
  return rx;
234
237
}
235
238
 
236
239
static int
255
258
    bytes = Curl_base64_encode(conn->data, (char *)buf, bytes, &cmdbuf);
256
259
    if(bytes > 0) {
257
260
      if(protlevel == prot_private)
258
 
        block_write(fd, "ENC ", 4);
 
261
        block_write(fd, "ENC ", 4);
259
262
      else
260
 
        block_write(fd, "MIC ", 4);
 
263
        block_write(fd, "MIC ", 4);
261
264
      block_write(fd, cmdbuf, bytes);
262
265
      block_write(fd, "\r\n", 2);
263
 
      Curl_infof(conn->data, "%s %s\n", protlevel == prot_private ? "ENC" : "MIC", cmdbuf);
 
266
      Curl_infof(conn->data, "%s %s\n",
 
267
                 protlevel == prot_private ? "ENC" : "MIC", cmdbuf);
264
268
      free(cmdbuf);
265
269
    }
266
 
  } else {
 
270
  }
 
271
  else {
267
272
    bytes = htonl(bytes);
268
273
    block_write(fd, &bytes, sizeof(bytes));
269
274
    block_write(fd, buf, ntohl(bytes));
278
283
  if(conn->data_prot != prot_clear) {
279
284
    if(conn->out_buffer.index > 0){
280
285
      Curl_sec_write(conn, fd,
281
 
                conn->out_buffer.data, conn->out_buffer.index);
 
286
                     conn->out_buffer.data, conn->out_buffer.index);
282
287
      conn->out_buffer.index = 0;
283
288
    }
284
289
    sec_send(conn, fd, NULL, 0);
457
462
    void *tmp;
458
463
 
459
464
    tmp = realloc(conn->app_data, (*m)->size);
460
 
    if (tmp == NULL) {
 
465
    if(tmp == NULL) {
461
466
      failf (data, "realloc %u failed", (*m)->size);
462
467
      return -1;
463
468
    }
518
523
void
519
524
Curl_sec_end(struct connectdata *conn)
520
525
{
521
 
  if (conn->mech != NULL) {
 
526
  if(conn->mech != NULL) {
522
527
    if(conn->mech->end)
523
528
      (conn->mech->end)(conn->app_data);
524
529
    memset(conn->app_data, 0, conn->mech->size);